My XML is like:
我的XML就像:
<root>
<section name="blah">
<item name="asdf">2222</item>
</section>
</root>
I will have multiple 'sections' in the XML, I want to fetch a particular section.
我将在XML中有多个“部分”,我想获取特定部分。
In this case, I need to get items that are in the section named "blah".
在这种情况下,我需要获取名为“blah”的部分中的项目。
1 个解决方案
#1
The xpath is then:
然后是xpath:
/root/section[@name='blah']/item
for example, in XmlDocument
:
例如,在XmlDocument中:
foreach(XmlElement item in doc.SelectNodes("/root/section[@name='blah']/item"))
{
Console.WriteLine(item.GetAttribute("name"));
Console.WriteLine(item.InnerText);
}
Edit re comments: if you just want the sections, then use:
编辑重新评论:如果您只想要这些部分,那么使用:
/root/section[@name='blah']
but then you'll need to iterate the data manually (since you can theoretically have multiple sections named "blah", each of which can have multiple items).
但是你需要手动迭代数据(因为你理论上可以有多个名为“blah”的部分,每个部分可以有多个项目)。
#1
The xpath is then:
然后是xpath:
/root/section[@name='blah']/item
for example, in XmlDocument
:
例如,在XmlDocument中:
foreach(XmlElement item in doc.SelectNodes("/root/section[@name='blah']/item"))
{
Console.WriteLine(item.GetAttribute("name"));
Console.WriteLine(item.InnerText);
}
Edit re comments: if you just want the sections, then use:
编辑重新评论:如果您只想要这些部分,那么使用:
/root/section[@name='blah']
but then you'll need to iterate the data manually (since you can theoretically have multiple sections named "blah", each of which can have multiple items).
但是你需要手动迭代数据(因为你理论上可以有多个名为“blah”的部分,每个部分可以有多个项目)。