如何使用 Linq 解析 xml?

发布于 2024-11-03 09:08:32 字数 839 浏览 1 评论 0原文

您好,我有 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 类中

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

public class Item()
{
     public string attribute{get;set;}
     public string subattribute{get;set;}
     public string subattribute2{get;set;}
}

如何使用 LINQ 来完成此操作?

Hi I have xml string like you can see below

 <?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>

I would like to get each item in allitems element get attribute values and add them to Enumerable class

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

public class Item()
{
     public string attribute{get;set;}
     public string subattribute{get;set;}
     public string subattribute2{get;set;}
}

How it can be done with using LINQ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没企图 2024-11-10 09:08:32

您的示例在 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));
}

Your example would look like this in 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));
}
活泼老夫 2024-11-10 09:08:32

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

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
}

Here's something that will directly translate into your class:

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
}
相对绾红妆 2024-11-10 09:08:32

您有LINQ to XML,您可以使用它通过 LINQ 查询 XML

You have LINQ to XML which you can use to query XML with LINQ

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文