C# 读取每个特定节点的所有XML子节点
我需要读取我的C# 读取每个特定节点的所有XML子节点,c#,linq,C#,Linq,我需要读取我的标记的所有子节点,问题是我的XML文件中有多个标记,每个标记之间的差异是一个名为ID的属性 这是一个例子 <Imoveis> <Imovel id="555"> <DateImovel>2012-01-01 00:00:00.000</DateImovel> <Pictures> <Picture> <Path>hhhhh
标记的所有子节点
,问题是我的XML文件中有多个
标记,每个
标记之间的差异是一个名为ID的属性
这是一个例子
<Imoveis>
<Imovel id="555">
<DateImovel>2012-01-01 00:00:00.000</DateImovel>
<Pictures>
<Picture>
<Path>hhhhh</Path>
</Picture>
</Pictures>
// Here comes a lot of another tags
</Imovel>
<Imovel id="777">
<DateImovel>2012-01-01 00:00:00.000</DateImovel>
<Pictures>
<Picture>
<Path>tttt</Path>
</Picture>
</Pictures>
// Here comes a lot of another tags
</Imovel>
</Imoveis>
但是,在我的foreach
语句中,由于我的
标记,她的父标记没有ID属性,因此工作不太好
有人能帮我做这个方法吗?你应该可以用两个foreach语句来做:
foreach(var imovel in doc2.Root.Descendants("Imovel"))
{
//Do something with the Imovel node
foreach(var children in imovel.Descendants())
{
//Do something with the child nodes of Imovel.
}
}
试试这个。XPath将向XElement添加XPath选择器。使用xpath查找元素更快、更简单 加载文件不需要XmlReader&XDocument
XElement root = XElement.Load("test.xml");
foreach (XElement imovel in root.XPathSelectElements("//Imovel"))
{
foreach (var children in imovel.Descendants())
{
String name = children.Name.LocalName;
String value = children.Value;
Console.WriteLine("Name:{0}, Value:{1}", name, value);
}
//use relative xpath to find a child element
XElement picturePath = imovel.XPathSelectElement(".//Pictures/Picture/Path");
Console.WriteLine("Picture Path:{0}", picturePath.Value);
}
请包括
System.Xml.XPath;
但在我的foreach语句中效果不太好。
你能解释一下这是什么意思吗?是的,这是因为我的
标记的父项没有ID属性
System.Xml.XPath;