C# 如何使用Linq解析xml?

C# 如何使用Linq解析xml?,c#,linq-to-xml,C#,Linq To Xml,您好,我有xml字符串,您可以在下面看到 <?xml version="1.0" encoding="ISO-8859-1" ?> - <Result> - <AllItems> - <Item attribute="123"> <SubItem subAttribute="1" /> <SubItem2 subAttribute2="2" /&

您好,我有xml字符串,您可以在下面看到

 <?xml version="1.0" encoding="ISO-8859-1" ?> 
- <Result>
    - <AllItems>
        - <Item attribute="123">
              <SubItem subAttribute="1" /> 
              <SubItem2 subAttribute2="2" /> 
          </Item>
        - <Item attribute="321">
              <SubItem subAttribute="3" /> 
              <SubItem2 subAttribute2="4" /> 
          </Item>
      </AllItems>
  </Result>

- 
- 
- 
- 
我想获取allitems元素中的每个项,获取属性值并将它们添加到可枚举类中

Enumerable<Item> allitems = new Enumerable<Item>();

public class Item()
{
     public string attribute{get;set;}
     public string subattribute{get;set;}
     public string subattribute2{get;set;}
}
Enumerable allitems=新的可枚举项();
公共类项目()
{
公共字符串属性{get;set;}
公共字符串子属性{get;set;}
公共字符串子属性2{get;set;}
}

如何使用LINQ进行查询?

您可以使用LINQ查询XML

您可以使用LINQ查询XML

您的示例在LINQ to XML中如下所示:

XDocument doc = XDocument.Load("test.xml");
var allItems = doc.Descendants("Item").Select(x => new Item() 
{
    attribute = x.Attribute("attribute").Value,
    subattribute = x.Element("SubItem").Attribute("subAttribute").Value,
    subattribute2 = x.Element("SubItem2").Attribute("subAttribute2").Value
});

foreach (var item in allItems)
{
    Console.WriteLine(string.Format("{0}: {1} / {2} ", item.attribute, 
                                                       item.subattribute, 
                                                       item.subattribute2));
}

在Linq to XML中,您的示例如下所示:

XDocument doc = XDocument.Load("test.xml");
var allItems = doc.Descendants("Item").Select(x => new Item() 
{
    attribute = x.Attribute("attribute").Value,
    subattribute = x.Element("SubItem").Attribute("subAttribute").Value,
    subattribute2 = x.Element("SubItem2").Attribute("subAttribute2").Value
});

foreach (var item in allItems)
{
    Console.WriteLine(string.Format("{0}: {1} / {2} ", item.attribute, 
                                                       item.subattribute, 
                                                       item.subattribute2));
}

以下内容将直接转化为您的课堂:

var allItems = from item in XDocument.Load("myxmlfile.xml").Descendants("Item")
    select new Item()
    {
        attribute = (string)item.Attribute("attribute"),
        subattribute = (string)item.Element("SubItem1").Attribute("subAttribute"),
        subattribute2 = (string)item.Element("SubItem2").Attribute("subAttribute")
    };

foreach(Item item in allItems)
{
    // your logic here
}

以下内容将直接转化为您的课堂:

var allItems = from item in XDocument.Load("myxmlfile.xml").Descendants("Item")
    select new Item()
    {
        attribute = (string)item.Attribute("attribute"),
        subattribute = (string)item.Element("SubItem1").Attribute("subAttribute"),
        subattribute2 = (string)item.Element("SubItem2").Attribute("subAttribute")
    };

foreach(Item item in allItems)
{
    // your logic here
}

不存在
可枚举的类
。改为使用
列表
。可能的重复项不存在
可枚举
类。使用
列表
代替。可能重复的