读取特定的XML节点

卢卡斯·桑托斯(Lucas Santos)

如何选择XML文件中的特定节点

在我的第一个操作中,foreach我选择PropertyProperties标记中的每个标记,但是我想要一个特定的标记Property例如PropertyA<PropertyCode>等于123。

XML格式

<Carga>
    <Properties>
       <Property>
           <PropertyCode>122</PropertyCode>
           <Fotos>
               <Foto>
               </Foto>
           </Fotos>
       </Property>
       <Property>
           <PropertyCode>123</PropertyCode>
           <Fotos>
               <Foto>
               </Foto>
           </Fotos>
       </Property>
     </Properties>
</Carga>

C#代码

// Here I get all Property tag
// But i want to take just a specific one
foreach (XmlElement property in xmldoc.SelectNodes("/Carga/Properties/Property"))
{
   foreach (XmlElement photo in imovel.SelectNodes("Fotos/Foto"))
   {
       string photoName = photo.ChildNodes.Item(0).InnerText.Trim();
       string url = photo.ChildNodes.Item(1).InnerText.Trim();
       this.DownloadImages(property_id, url, photoName );
   }
}

有人可以帮我吗?

谢尔盖·别列佐夫斯基(Sergey Berezovskiy)

使用Linq转换为Xml

int code = 123;
var xdoc = XDocument.Load(path_to_xml);
var property = xdoc.Descendants("Property")
                   .FirstOrDefault(p => (int)p.Element("PropertyCode") == code);

if (property != null)
{
    var fotos = property.Element("Fotos").Elements().Select(f => (string)f);
}

fotos 将是字符串的集合。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章