如何查询这个结构的xml?

时间:2021-10-13 14:31:03
 <root>
    <level1>
        <item id="1" date="" name="" >
        <item id="2" date="" name="" >
        <item id="3" date="" name="" >
        <item id="4" date="" name="" >
        <item id="5" date="" name="" >
    </level1>
 </root>

I have an xml structure like the one above.

我有一个像上面那样的xml结构。

I used

我用了

XmlNodeList xnList = xmlDoc.SelectNodes("/level1");

If I used xmlnodelist as above, how can I specifically only get the element with id="3"?

如果我如上所述使用xmlnodelist,我怎样才能专门获取id =“3”的元素?

or more useful if I could store all elements inside as elements in xnlist?

或者更有用,如果我可以将所有元素存储在xnlist中的元素中?

2 个解决方案

#1


2  

XmlNodeList xnList = xmlDoc.SelectNodes("//level1/item[@id='3']");

and if you want to use Linq To Xml

如果你想使用Linq To Xml

var xDoc = XDocument.Parse(xmlstring); // XDocument.Load(filename)
var items = xDoc.Descendants("level1")
                .First()
                .Elements("item")
                .Select(item => new { 
                                    ID = item.Attribute("id").Value, 
                                    Name = item.Attribute("name").Value 
                                })
                .ToList();

You can even combine XPath and Linq2Xml

您甚至可以组合XPath和Linq2Xml

var item2 = xDoc.XPathSelectElements("//level1/item")
                .Select(item => new { 
                                    ID = item.Attribute("id").Value, 
                                    Name = item.Attribute("name").Value 
                                })
                .ToList();

#2


2  

besides the great answer from @L.B I also use Linq, personally I think is a lot more readable:

除了来自@ L.B的优秀答案,我也使用Linq,我个人觉得它更具可读性:

xdoc.Element("level1")
    .Descendants("item")
    .Where(x => x.Attribute("id").Value == "3").First();

but it all depends on your style ;)

但这一切都取决于你的风格;)

#1


2  

XmlNodeList xnList = xmlDoc.SelectNodes("//level1/item[@id='3']");

and if you want to use Linq To Xml

如果你想使用Linq To Xml

var xDoc = XDocument.Parse(xmlstring); // XDocument.Load(filename)
var items = xDoc.Descendants("level1")
                .First()
                .Elements("item")
                .Select(item => new { 
                                    ID = item.Attribute("id").Value, 
                                    Name = item.Attribute("name").Value 
                                })
                .ToList();

You can even combine XPath and Linq2Xml

您甚至可以组合XPath和Linq2Xml

var item2 = xDoc.XPathSelectElements("//level1/item")
                .Select(item => new { 
                                    ID = item.Attribute("id").Value, 
                                    Name = item.Attribute("name").Value 
                                })
                .ToList();

#2


2  

besides the great answer from @L.B I also use Linq, personally I think is a lot more readable:

除了来自@ L.B的优秀答案,我也使用Linq,我个人觉得它更具可读性:

xdoc.Element("level1")
    .Descendants("item")
    .Where(x => x.Attribute("id").Value == "3").First();

but it all depends on your style ;)

但这一切都取决于你的风格;)