C# 使用LinQ解析XML后,列表为空

C# 使用LinQ解析XML后,列表为空,c#,xml,linq,C#,Xml,Linq,我有一个类似于以下内容的xml文件: <doc> <file> <header> <source> RNG </source> </header> <body> <item name="items.names.id1"> <property>propertyvalue1</propert

我有一个类似于以下内容的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>
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();
例如,该文件有100个条目(在“名称”属性中标记为1到100)。如何使用LINQXML获取这些节点并将它们放入项目列表中

以Selman22为例,我做了以下工作:

<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>
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,或者反序列化它并使用LINQ对象。如果您选择反序列化,我建议您从模式开始,使用生成表示您的数据模型的类。如果您没有xml的模式,即使是xsd.exe也可以从示例xml文件中推断出一个模式,但您可能需要微调结果。

尝试此模式
XElement root=XElement.Parse(“您的文件名”);
var items textSegs=(来自root.subjects(“项”)中的项)
选择项)

现在,使用
linqtoxml:

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();

下面是使用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 doc=Xdocument.Parse(输入);
var数据=单据子体(“项目”);
列表项列表=新列表();
foreach(数据中的var项)
{
字符串itemname=item.Element(“item”).Value;
字符串属性=item.Element(“属性”).Value;
添加(新项目(项目名称、属性));
}

我猜你想知道你的问题是如何表达的。。另外,我假设真正的XML也是非常简单的

var items = from item in doc.Descendants("item")
            select new Item()
            {
             Id = item.Attributes("name").First().Value,
             Property = item.Elements().First().Value,
            };
只需确保xml已加载到文档中。可以通过两种方式加载xml:

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

您发布的“XML”无效。另外,您尝试了什么?请阅读
XDocument.Parse
。您正在查找
XDocument.Load
。如果不是这样,请一直使用LinqToXml。