c# - Reaching a specific Xmlnode from root using XPath with nested indexes -


i have xml string have specific node

using system; using system.collections.generic; using system.collections; using system.linq; using system.web; using system.xml; using system.xml.xpath; 

//xml string has xml content

string xmlstr = "<candidate>     <content>     <dob>14-jan-1976</dob>     <employers>       <employer>         <name>diane trucking</name>         <addresses>           <address>             <addressline1>1st street</addressline1>             <addressline2/>             <city>first city</city>             <state>fl</state>             <zip>12345</zip>           </address>           <address>             <addressline1>1st street</addressline1>             <addressline2/>             <city>second city</city>             <state>fl</state>             <zip>12346</zip>           </address>           <address>             <addressline1>3rd street</addressline1>             <addressline2/>             <city>third city</city>             <state>fl</state>             <zip>12347</zip>           </address>         </addresses>       </employer>       <employer>         <name>tom trucking</name>         <addresses>           <address>             <addressline1>4th street</addressline1>             <addressline2/>             <city>fourth city</city>             <state>fl</state>             <zip>12348</zip>           </address>           <address>             <addressline1>5th street</addressline1>             <addressline2/>             <city>fifth city</city>             <state>fl</state>             <zip>12349</zip>           </address>           <address>             <addressline1>6th street</addressline1>             <addressline2/>             <city>sixth city</city>             <state>fl</state>             <zip>12340</zip>           </address>         </addresses>       </employer>     </employers>     </content>     </candidate>"; 

// create xml document

        xmldocument xmldoc = new xmldocument();         xmldoc.loadxml(xmlstr); 

navigator alternative testing

        xpathnavigator xpathnav = xmldoc.createnavigator();         xpathexpression expr = xpathexpression.compile("/candidate/content/employers['1']/employer['1']/addresses['1']/address"); 

expression read/target xml specific xml node

        xpathnodeiterator xpathit = xpathnav.select(expr);   if(xpathit == null) { 

... xpathit.count 6

}  var xmlnodes1 = xmldoc.selectnodes("/candidate/content/employers['1']/employer['1']/addresses['1']/address");  (int = 0; < xmlnodes1.count; i++ )   { 

...xmlnodes1.count = 6. returns 6 address nodes , expecting 3 nodes of "address" specific employers/employer/addresses/address

} 

both xpathit xmlnodes1 return 6 "address" elements expected return 3 elements there 3 addresses employer

i wanted target element in xml root also, if can able update specific element.

appreciate thank you

try removing single quotes xpath:

var xmlnodes1 = xmldoc.selectnodes("/candidate/content/employers['1']/employer['1']/addresses['1']/address"); 

should be

var xmlnodes1 = xmldoc.selectnodes("/candidate/content/employers[1]/employer[1]/addresses[1]/address"); 

note single quotes around number 1 removed.

the predicate selecting first item [1]. using predicate ['1'] ignored selectnodes getting all nodes , not first.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -