我设法从我的XML中检索所有数据并将其显示在我的网站上(正常工作)。但是,我不想显示我检索到的每一个数据。
我只想获取并显示一些特定的值,例如Customer - > Id,Name,Address和ShippingAddress - > 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>
CustomerController.cs
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.cs
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文件,因此更容易阅读
答案 0 :(得分:0)
试试这个
类应该是
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
的{{1}}循环中,如下所示
CustomerController.cs
CustomerController.cs
更新您的cshtml查询
1)ShippingAddress Model
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);
}
2)CustomerViewModel.cs
public class ShippingAddress
{
public string Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
3)cshtml
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; }
}