【问题标题】:List is empty after parsing XML with LinQ使用 LinQ 解析 XML 后列表为空
【发布时间】:2014-01-24 16:45:33
【问题描述】:

我有一个类似于以下的 xml 文件:

<doc>
  <file>
    <header>
      <source>
        RNG
      </source>
    </header>
    <body>
      <item name="items.names.id1">
         <property>propertyvalue1</property>
      </item>
      <!-- etc -->
      <item name="items.names.id100">
         <property>propertyvalue100</property>
      </item>
      <!-- etc -->
      <item name="otheritems.names.id100">
         <property>propertyvalue100</property>
      </item>
    </body>
  </file>
</doc>

还有以下类:

private class Item
{
    public string Id;
    public string Property;
}

例如,该文件有 100 个项目条目(在名称属性中标记为 1 到 100)。如何使用 Linq Xml 获取这些节点并将它们放入项目列表中?

使用 Selman22 的示例,我正在执行以下操作:

var myList = xDoc.Descendants("item")
                 .Where(x => x.Attributes("name").ToString().StartsWith("items.names.id"))
                 .Select(item => new Item
                 {
                     Id = (string)item.Attribute("name"),
                     Name = (string)item.Element("property")
                 }).ToList();

但是,列表是空的。我在这里错过了什么?

【问题讨论】:

  • 您发布的“XML”无效。另外,你试过什么?

标签: c# xml linq


【解决方案1】:

使用LINQ to XML:

XDocument xDoc = XDocument.Load(filepath);
var myList = xDoc.Descendants("item").Select(item => new Item {
                                      Id = (string)item.Attribute("name"),
                                      Property = (string)item.Element("property")
                                                              }).ToList();

【讨论】:

    【解决方案2】:

    您可以使用LinqToXml 直接查询XML,或者反序列化它并使用LINQ 来反对。如果您选择反序列化,我建议从架构开始并使用xsd.exe 生成代表您的数据模型的类。如果您没有 xml 的架构,即使 xsd.exe 也可以从示例 xml 文件中推断出一个,但您可能需要微调结果。

    【讨论】:

      【解决方案3】:

      试试这个XElement root = XElement.Parse("your file name"); var items textSegs =(from item in root.Descendants("item") select item).ToList(); 现在遍历列表并存储它

      【讨论】:

      • 那不是要走的路,一直使用LinqToXml。
      【解决方案4】:

      以下是使用 Xdocument 从 xml 获取信息的一种方式。

      string input = "<Your xml>";
      
      Xdocument doc = XDocument.Parse(input);
      
      var data = doc.Descendants("item");
      List<Items> itemsList = new List<Items>();
      
      foreach(var item in data)
      {
      string itemname= item.Element("item").Value;
      string property = item.Element("property").Value;
      itemsList.Add(new item(itemname, property));
      }
      

      【讨论】:

      • 请阅读XDocument.Parse。您正在寻找XDocument.Load
      【解决方案5】:

      我猜你想要代码给出你的问题是如何表达的。我还假设真正的 XML 也非常简单。

      var items = from item in doc.Descendants("item")
                  select new Item()
                  {
                   Id = item.Attributes("name").First().Value,
                   Property = item.Elements().First().Value,
                  };
      

      只需确保您的 xml 已加载到 doc 中。您可以通过两种方式加载 xml:

      // By a string with xml
      var doc = XDocument.Parse(aStringWithXml);
      // or by loading from uri (file)
      var doc = XDocuemnt.Load(aStringWhichIsAFile);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-13
        • 1970-01-01
        • 1970-01-01
        • 2012-01-20
        • 1970-01-01
        • 1970-01-01
        • 2012-05-29
        相关资源
        最近更新 更多