请有人可以帮助我反序列化具有相同元素名称的XML结构吗?使对象具有完全相同名称的元素的最佳实践是什么?
<RetrieveVehicleConsumersResponse>
<VehicleConsumerRelationship>
<Customer>
<CustomerID>
<FirstID>SomeData</FirstID>
<SecondID>SomeData</SecondID>
</CustomerID>
<CompanyName>SomeData</CompanyName>
<ContactName>SomeData</ContactName>
<ContactTitle>SomeData</ContactTitle>
<Address>SomeData</Address>
<City>SomeData</City>
<PostalCode>SomeData</PostalCode>
<Country>SomeData</Country>
<Phone>SomeData</Phone>
<Fax>SomeData</Fax>
</Customer>
<Customer>
<CustomerID>
<FirstID>SomeData</FirstID>
<SecondID>SomeData</SecondID>
</CustomerID>
<CompanyName>SomeData</CompanyName>
<ContactName>SomeData</ContactName>
<ContactTitle>SomeData</ContactTitle>
<Address>SomeData</Address>
<City>SomeData</City>
<PostalCode>SomeData</PostalCode>
<Country>SomeData</Country>
<Phone>SomeData</Phone>
<Fax>SomeData</Fax>
</Customer>
</VehicleConsumerRelationship>
</RetrieveVehicleConsumersResponse>
在这里您可以找到我的C#代码。不幸的是,它不起作用。客户元素不在对象中。
[XmlRoot("RetrieveVehicleConsumersResponse")]
public class IdentifyData
{
public VehicleConsumerRelationship VehicleConsumerRelationship { get; set; }
}
public class VehicleConsumerRelationship
{
[XmlArray("VehicleConsumerRelationship")]
[XmlArrayItem("Customer")]
public List<Customer> Customer { get; set; }
}
public class Customer
{
public string CompanyName { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var sr = new StreamReader("file name"))
{
var xs = new XmlSerializer(typeof(IdentifyData));
var responseData = (IdentifyData)xs.Deserialize(sr);
}
}
}
预期结果是将所有Customer元素和相关的子元素放入一个对象。
谢谢。