【发布时间】:2012-08-16 19:08:48
【问题描述】:
我需要阅读我的<Imovel>标签的所有Child Nodes,问题是我的XML文件中有不止1(一个)<Imovel>标签,每个<Imovel>标签之间的区别是一个名为 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>
我需要读取每个<Imovel> 标签的所有标签,并且在我在<Imovel> 标签中进行的每次验证结束时,我需要进行另一次验证。
所以,我认为我需要做 2(两个)foreach 或 for 和 foreach,我不太了解 LINQ,但请按照我的示例进行操作
XmlReader rdr = XmlReader.Create(file);
XDocument doc2 = XDocument.Load(rdr);
ValidaCampos valida = new ValidaCampos();
//// Here I Count the number of `<Imovel>` tags exist in my XML File
for (int i = 1; i <= doc2.Root.Descendants().Where(x => x.Name == "Imovel").Count(); i++)
{
//// Get the ID attribute that exist in my `<Imovel>` tag
id = doc2.Root.Descendants().ElementAt(0).Attribute("id").Value;
foreach (var element in doc2.Root.Descendants().Where(x => x.Parent.Attribute("id").Value == id))
{
String name = element.Name.LocalName;
String value = element.Value;
}
}
但是效果不太好,在我的foreach语句中因为我的<Picture>标签,她的父标签没有ID属性。
有人可以帮我做这个方法吗?
【问题讨论】:
-
But doesn't work very well, in my foreach statement.你能解释一下你的意思吗? -
是的,这是因为我的
<Picture>标签的父级没有 ID 属性