Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)

XML
<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>


I need help with the xpath query to find a match or if there is another way to do this
thanks for your help,
Posted
Updated 10-Jun-15 19:40pm
v2
Comments
ramyajaya 11-Jun-15 1:23am    
Refer this as how to query XML doc.

https://msdn.microsoft.com/en-us/library/bb675161.aspx
Afrikiko 11-Jun-15 2:05am    
thank you so much.

1 solution

Yes, there is another way, via using XDocument class[^].

C#
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);


If you want to get result using XPath class, please refer this: Select Nodes Using XPath Navigation[^]

Try!
 
Share this answer
 
v2
Comments
Afrikiko 11-Jun-15 2:04am    
Thank you so much.
Maciej Los 11-Jun-15 2:07am    
You're very welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900