我需要验证节点是否存在, [英] I need to verify if a node exist,
本文介绍了我需要验证节点是否存在,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个xml,我需要验证节点是否存在,如果节点不存在,我必须添加它。我需要匹配loc( http://192.168.6.22/Conditions.aspx
)
I have this xml and I need to verify if a node exist, if a node does not exist i have to add it. I need to match on loc(http://192.168.6.22/Conditions.aspx
)
<urlset xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://192.168.6.22/Richard/default.aspx</loc>
<lastmod>2015-01-19</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://192.168.6.22/Nelson/default.aspx</loc>
<lastmod>2015-06-11</lastmod>
<changefreq>weekly</changefreq>
</url>
<url>
<loc>https://192.168.6.22/Michael/default.aspx</loc>
<lastmod>2015-06-11</lastmod>
<changefreq>weekly</changefreq>
</url>
<url>
<loc>https://192.168.6.22/Edwards/default.aspx</loc>
<lastmod>2015-06-11</lastmod>
<changefreq>weekly</changefreq>
</url>
</urlset>
我需要xpath查询的帮助才能找到匹配项,或者是否有其他方法可以做到这一点
感谢您的帮助,
I need help with the xpath query to find a match or if there is another way to do this
thanks for your help,
推荐答案
是的,还有另一种方法,通过使用 XDocument类 [ ^ ]。
Yes, there is another way, via using XDocument class[^].
string sloc = "http://192.168.6.22/Conditions.aspx";
XDocument xdoc = XDocument.Load("fullfilename.xml");
XNamespace xnsp = "http://www.sitemaps.org/schemas/sitemap/0.9";
//return loc node - if exists
var qry = xdoc.Descendants(xnsp + "loc")
.Where(a=>(string)a==sloc);
//or return boolean value (true/false)
//bool result = xdoc.Descendants(xnsp + "loc")
// .Any(a=>(string)a==sloc);
如果你想使用XPath类获得结果,请参考:使用XPath导航选择节点 [ ^ ]
试试!
If you want to get result using XPath class, please refer this: Select Nodes Using XPath Navigation[^]
Try!
这篇关于我需要验证节点是否存在,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文