c# - Get nodes from xml files -


how parse xml file?

<?xml version="1.0" encoding="utf-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">  <sitemap>      <loc>link</loc>     <lastmod>2011-08-17t08:23:17+00:00</lastmod>  </sitemap>  <sitemap>     <loc>link</loc>      <lastmod>2011-08-18t08:23:17+00:00</lastmod>  </sitemap>  </sitemapindex> 

i new xml, tried this, seems not working :

        xmldocument xml = new xmldocument(); //* create xml document object.          xml.load("sitemap.xml");         xmlnodelist xnlist = xml.selectnodes("/sitemapindex/sitemap");         foreach (xmlnode xn in xnlist)         {             string loc= xn["loc"].innertext;             string lastmod= xn["lastmod"].innertext;         } 

the problem sitemapindex element defines default namespace. need specify namespace when select nodes, otherwise not find them. instance:

xmldocument xml = new xmldocument(); xml.load("sitemap.xml"); xmlnamespacemanager manager = new xmlnamespacemanager(xml.nametable); manager.addnamespace("s", "http://www.sitemaps.org/schemas/sitemap/0.9"); xmlnodelist xnlist = xml.selectnodes("/s:sitemapindex/s:sitemap", manager); 

normally speaking, when using xmlnamespacemanager, leave prefix empty string specify want namespace default namespace. think you'd able this:

// won't work xmldocument xml = new xmldocument(); xml.load("sitemap.xml"); xmlnamespacemanager manager = new xmlnamespacemanager(xml.nametable); manager.addnamespace("", "http://www.sitemaps.org/schemas/sitemap/0.9"); //empty prefix xmlnodelist xnlist = xml.selectnodes("/sitemapindex/sitemap", manager); //no prefixes in xpath 

however, if try code, you'll find won't find matching nodes. reason in xpath 1.0 (which xmldocument implements), when no namespace provided, uses null namespace, not default namespace. so, doesn't matter if specify default namespace in xmlnamespacemanager, it's not going used xpath, anyway. quote relevant paragraph official xpath specification:

a qname in node test expanded expanded-name using namespace declarations expression context. same way expansion done element type names in start , end-tags except default namespace declared xmlns not used: if qname not have prefix, namespace uri null (this same way attribute names expanded). error if qname has prefix there no namespace declaration in expression context.

therefore, when elements reading belong namespace, can't avoid putting namespace prefix in xpath statements. however, if don't want bother putting namespace uri in code, can use xmldocument object return uri of root element, in case, want. instance:

xmldocument xml = new xmldocument(); xml.load("sitemap.xml"); xmlnamespacemanager manager = new xmlnamespacemanager(xml.nametable); manager.addnamespace("s", xml.documentelement.namespaceuri); //using xml's properties instead of hard-coded uri xmlnodelist xnlist = xml.selectnodes("/s:sitemapindex/s:sitemap", manager); 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -