代码之家  ›  专栏  ›  技术社区  ›  Shaun

如何使用ASP从XML文件中检索特定数据。网

  •  0
  • Shaun  · 技术社区  · 6 年前

    我设法从XML中检索所有数据,并将其显示在我的网站上(正常工作)。然而,我不想显示我检索到的每一个数据。

    我只想获取并显示一些特定的值,例如Customer->Id、名称、地址和发货地址->Id、姓名和地址(我有一个基于xml的列表)。

    XML结构

    <?xml version="1.0" encoding="windows-1252"?>
    <Response>
      <Result>
        <TotalCount>1</TotalCount>
        <Customer>
          <field name="Id" value="1234" />
          <field name="Name" value="Dr. Strange" />
          <field name="Address" value="122 New York" />
          <field name="Address2" value="" />
          <field name="Phone" value="3333333333" />
          <field name="ZipCode" value="V5C 6N5" />
          <ShippingAddresses>
            <ShippingAddress>
              <field name="Id" value="456" />
              <field name="Name" value="Wong" />
              <field name="Address" value="122 Drive" />
              <field name="Address2" value="" />
              <field name="ZipCode" value="V5C 6N5" />
           </ShippingAddress>
        </ShippingAddresses>
        </Customer>
      </Result>
      <Errors />
    </Response>
    

    客户控制器。反恐精英

    XmlNodeList customerNodes = xmlDocument.SelectNodes("// Customer");
    List<CustomerViewModel> customerList = new List<CustomerViewModel>();
    CustomerViewModel customer = new CustomerViewModel();
    
    foreach (XmlNode node in customerNodes)
    { 
      XmlNodeList fieldNodes = node.SelectNodes("field");
      foreach (XmlNode fieldNode in fieldNodes)
      {
        string attrName = fieldNode.Attributes["name"].Value;
        string attrValue = fieldNode.Attributes["value"].Value;
        if (customer.GetType().GetProperty(attrName) != null)
        {
          customer.GetType().GetProperty(attrName).SetValue(customer, attrValue);
        }
      }
      customerList.Add(customer);
    }
    

    CustomerViewModel。反恐精英

    public string Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Address2 { get; set; }
    public string Phone { get; set; }
    public string ZipCode { get; set; }
    

    目前,我的 customerList 包含所有内容,我可以显示每个值。我只需要显示选定的信息。

    注意:我刚刚修剪了我的XML文件,所以它更容易阅读

    1 回复  |  直到 6 年前
        1
  •  0
  •   Trimantra Software Solution    6 年前

    试试这个

    课程应该是

    public class Customer
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
            public List<ShippingAddress> ShippingAddresses { get; set; }
        }
        public class ShippingAddress
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
        }
    

    逻辑应该是

    XmlNodeList nodeList = doc.SelectNodes("// Customer");
                List<Customer> customers = new List<Customer>();
                foreach (XmlNode node in nodeList)
                {
                    Customer customer = new Customer();
                    XmlNodeList fieldNodes = node.SelectNodes("field");
                    foreach (XmlNode fieldNode in fieldNodes)
                    {
                        string attrName = fieldNode.Attributes["name"].Value;
                        string attrValue = fieldNode.Attributes["value"].Value;
                        if (customer.GetType().GetProperty(attrName)!=null)
                        {
                            customer.GetType().GetProperty(attrName).SetValue(customer, attrValue);
                        }
                    }
                    XmlNodeList shippingNodes = node.SelectNodes("ShippingAddresses/ShippingAddress");
                    List<ShippingAddress> ShippingAddresses = new List<ShippingAddress>();
                    foreach (XmlNode shippingNode in shippingNodes)
                    {
                        ShippingAddress ShippingAddress = new ShippingAddress();
                        XmlNodeList shippingFieldNodes = shippingNode.SelectNodes("field");
                        foreach (XmlNode shippingFieldNode in shippingFieldNodes)
                        {
                            string attrName = shippingFieldNode.Attributes["name"].Value;
                            string attrValue = shippingFieldNode.Attributes["value"].Value;
                            if (ShippingAddress.GetType().GetProperty(attrName) != null)
                            {
                                ShippingAddress.GetType().GetProperty(attrName).SetValue(ShippingAddress, attrValue);
                            }
                        }
                        ShippingAddresses.Add(ShippingAddress);
                    }
                    customer.ShippingAddresses = ShippingAddresses;
                    customers.Add(customer);
                }
    

    更新

    你应该把 CustomerViewModel customer = new CustomerViewModel(); foreach 循环 CustomerController.cs 如下所示

    客户控制器。反恐精英

    XmlNodeList customerNodes = xmlDocument.SelectNodes("// Customer");
    List<CustomerViewModel> customerList = new List<CustomerViewModel>();
    
    foreach (XmlNode node in customerNodes)
    { 
      CustomerViewModel customer = new CustomerViewModel();
      XmlNodeList fieldNodes = node.SelectNodes("field");
      foreach (XmlNode fieldNode in fieldNodes)
      {
        string attrName = fieldNode.Attributes["name"].Value;
        string attrValue = fieldNode.Attributes["value"].Value;
        if (customer.GetType().GetProperty(attrName) != null)
        {
          customer.GetType().GetProperty(attrName).SetValue(customer, attrValue);
        }
      }
      customerList.Add(customer);
    }
    

    更新cshtml查询

    1) ShippingAddress模型

    public class ShippingAddress
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
        }
    

    2) CustomerViewModel。反恐精英

    public class CustomerViewModel{
        public string Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Address2 { get; set; }
        public string Phone { get; set; }
        public string ZipCode { get; set; }
        public List<ShippingAddress> ShippingAddresses { get; set; }
    }
    

    @model CustomerViewModel
    .
    .
    .
    .
    @foreach(var shippingAddress in Model.ShippingAddresses)
    {
        <label>@shippingAddress.Name</label>
    }