【问题标题】:How to convert multi-level xml into objects using LINQ to XML?如何使用 LINQ to XML 将多级 xml 转换为对象?
【发布时间】:2013-04-22 22:22:15
【问题描述】:

我的 XML 文件:

<myobject property1="foo" property2="bar">
    <property3>value1</property3>
    <property3>value1</property3>
    <property3>value1</property3>
</myobject>

我的 C# 代码:

List<MyObject> myObjectsInDB = (from f in xmlDoc.Descendants("myobject")
             select new MyObject()
             {
                    Property1 = f.Attribute("property1").Value,
                    Property2 = f.Attribute("property2").Value,
                    // Property3 = f.Element("property3").Value,
             }).ToList();

如果您在 xml 文件中注意到我有 3 个元素需要转换为 C# 类以及 myobject 元素及其属性。访问 xml.xml 中的单个对象的最佳方法是什么?我知道我可能只运行一个单独的选择,但我想知道是否有更好的方法来访问它们,这样我就不必将所有内容都运行两次。

【问题讨论】:

    标签: c# .net xml linq-to-xml


    【解决方案1】:
    var result = xmlDoc.Descendants("myobject")
                    .Select(m => new
                    {
                        Property1 = m.Attribute("property1").Value,
                        Property2 = m.Attribute("property2").Value,
                        Property3 = m.Descendants("property3").Select(p3=>p3.Value).ToList()
                    })
                    .ToList();
    

    【讨论】:

      【解决方案2】:
      var myobjects = 
          from myobjectEl in xdoc.Elements("myobject")
          select new 
          {
              Property1 = myobjectEl.Attribute("property1").Value,
              Property2 = myobjectEl.Attribute("property1").Value,
              Property3Texts = 
                  (from prop3El in myobjectEl.Elements("property3") 
                  select prop3El.Value).ToList(),
          };
      

      顺便说一句:Descendants("x") 返回所有名为“x”的子元素,Elements("x") 返回所有名为“x”的立即子元素。

      【讨论】:

        【解决方案3】:

        假设:MyObject 已经被定义为类类型(见下文)。

        然后您可以将您的 xml 反序列化为一个对象,如下所示:

        public static MyObject deserializeMyObject(){
        
        var xmlString = @"<?xml version=""1.0"" ?><MyObject property1=""foo"" property2=""bar"">
            <property3>value1</property3>
            <property3>value1</property3>
            <property3>value1</property3>
        </MyObject>";
        var xdoc=XDocument.Parse(xmlString);
        XmlSerializer _s = new XmlSerializer(typeof(MyObject));
        var foo= (MyObject)_s.Deserialize(xdoc.CreateReader()); 
        return foo;
        }
        
        //assumption about the structure of your MyObject class
        public class MyObject{
         [XmlAttribute("property1")]
        public string property1{get;set;}
        [XmlAttribute("property2")]
        public string property2 {get;set;}
         [XmlElement]
        public string[] property3 {get;set;}
        }
        

        希望对你有帮助。

        【讨论】:

        • 使用 XmlSerializer 对性能的影响如何?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-22
        • 2012-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多