gpt4 book ai didi

c# - 读取特定的 XML 节点

转载 作者:太空宇宙 更新时间:2023-11-03 21:45:03 25 4
gpt4 key购买 nike

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

在我的第一个foreach我选择每个 Property在我的里面 Properties标签,但我想要一个特定的 Property . Property<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 );
}
}

有人可以帮助我吗?

最佳答案

使用Linq to 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 将是字符串的集合。

关于c# - 读取特定的 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17491283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com