从动态xml转换为c#对象

时间:2013-05-20 12:33:40

标签: c# .net c#-4.0 dynamic expandoobject

我需要输入将动态xml转换为定义的c#对象模型

我的样本xml如下:

<?xml version="1.0" encoding="utf-8" ?>
  <Persons>
    <Person>
      <Id>10</Id>
      <FirstName> Dino </FirstName>
      <LastName> Esposito </LastName>
      <Addresses>
        <Address>
          <AddressType>BillTo</AddressType>
          <Street1></Street1>
          <Street2></Street2>
          <Street3></Street3>
          <City>Moscow</City>
        </Address>
        <Address>
          <AddressType>ShipTo</AddressType>
          <Street1></Street1>
          <Street2></Street2>
          <Street3></Street3>
          <City>Moscow</City>
        </Address>
        <Address>
          <AddressType>Contact</AddressType>
          <Street1></Street1>
          <Street2></Street2>
          <Street3></Street3>
          <City>Moscow</City>
        </Address>
      </Addresses>
    </Person>
  </Persons>

我希望在运行时将此xml的值转换为C#对象。 我希望定义一个类似于以下的对象: 我的预期类对象C#如下:

public class Person
{
    public int Id { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public IList<Address> Addresses { get; set; }
}

public class Address
{
    public string AddressType { get; set; }
    public string Street1 { get; set; }
    public string Street2 { get; set; }
    public string Street3 { get; set; }
    public string City { get; set; }
}

我在C#4.0中经历了动态和ExpandoObject,这种方法允许我通过使用键动态获取值。我不知道如何将这些填充到我的数据模型中。

注意:我不想定义这个类模型结构,这会在一段时间内不断变化。我正在寻找一个解决方案,它允许我获取像DynamicObject.Addresses.Address [0] .City。

这样的值。

请提供您的意见。

5 个答案:

答案 0 :(得分:6)

使用LINQ2XML的解决方案可能如下所示(不需要类):

var xml = XDocument.Parse(xmlSrc); // from XML string, e.g.: <xml ...><Persons><Person>...
//var xml = XDocument.Load(xmlFile); // from XML file, e.g.: c:\temp\persons.xml

var persons = xml.Root.Elements("Person").ToList();
var p1Addresses = persons[0].Elements("Addresses").ToList();
foreach (var address in p1Addresses.Elements("Address"))
{
    var elementAddress = address.Element("AddressType");
    var elementCity = address.Element("City");
    System.Console.WriteLine(string.Format("{0} - {1}", elementAddress.Value, elementCity.Value));
}

输出结果为:

BillTo - Moscow
ShipTo - Moscow
Contact - Moscow

答案 1 :(得分:4)

我建议你阅读这篇文章:http://www.codeproject.com/Articles/62839/Adventures-with-C-4-0-dynamic-ExpandoObject-Elasti。有一种方法可以从XML构造动态对象。 或者这篇文章:http://www.codeproject.com/Articles/461677/Creating-a-dynamic-object-from-XML-using-ExpandoOb。随心所欲,您可以根据需要自定义代码。

答案 2 :(得分:2)

检查此示例:

        string xml =
            @"<?xml version='1.0' encoding='utf-8' ?>
              <Persons>
               <Person>
                <Id>10</Id>
                <FirstName> Dino </FirstName>
                <LastName> Esposito </LastName>
                <Addresses>
                  <Address>
                   <AddressType>BillTo</AddressType>
                   <Street1></Street1>
                   <Street2></Street2>
                   <Street3></Street3>
                   <City>Moscow</City>
                </Address>
                <Address>
                 <AddressType>ShipTo</AddressType>
                 <Street1></Street1>
                 <Street2></Street2>
                 <Street3></Street3>
                 <City>Moscow</City>
                </Address>
                <Address>
                  <AddressType>Contact</AddressType>
                  <Street1></Street1>
                  <Street2></Street2>
                  <Street3></Street3>
                  <City>Moscow</City>
                </Address>
             </Addresses>
            </Person>
           </Persons>";

        XElement root = XElement.Parse(xml);

        IEnumerable<XElement> list = root.XPathSelectElements("./Person/Addresses/Address[2]/City");
        foreach (XElement el in list)
            Console.WriteLine(el);
        Console.ReadLine();

您将获得:<City>Moscow</City>

或者使用DynamicObject

查看此解决方案
    XElement root = XElement.Parse(xml);
    dynamic persons = DynamicXml.Parse(xml);
    Console.WriteLine(persons.Person.Addresses.Address[1].City);

Deserialize XML To Object using Dynamic

答案 3 :(得分:1)

感谢大家的投入,我在这个位置可以找到我想要的解决方案 http://www.codeproject.com/Tips/227139/Converting-XML-to-an-dynamic-object-using-ExpandoO

答案 4 :(得分:0)

我已经专门为此目的创建了一个项目,因为我对替代方案不满意(例如在其他答案中链接的CodeProject文章)。

我已将它发布在Github上:https://github.com/jonathanconway/XmlToDynamic

我不想在这里发布源代码,因为它太大了,但这是用法:

var xmlString =
    @"<addressBook>
       <contacts>
         <contact>
           <name>Jonathan</name>
         </contact>
       </contacts>
     </addressBook>";
var dynamicXml = XElement.Parse(xmlString).ToDynamic();
var firstPersonsName = dynamicXml.contacts[0].name.Value;
// firstPersonsName will be 'Jonathan'
相关问题