Looping through all nodes in xml file with c# -


i have xml document setup similiar this:

<invoice>    <issuedate>2015-09-07</issuedate>    <invoicetype>380<invoicetype>    <accountingsupplierparty>         <party>              <endpointid></endpointid>              <partyname>                   <name>company test</name>              </partyname>         </party>     </accountingsupplierparty> </invoice> 

this little piece of entire xml document, show how file looks.

i check elements see if have empty values, such endpointid in example (i need replace empty values na).

this have far:

public static void addtoemptyelements() {     xmldocument xmldoc = new xmldocument();     xmldoc.load("testxml.xml");     xmlnodelist nodes = xmldoc.documentelement.childnodes;     foreach (xmlnode node in nodes)     {         console.writeline(node.name);                  } } 

however, code loop through childs of root node , not grandchilds (or great grandchilds) such <party> , <endpointid> elements. how can include these elements in loop?

i'm not in environment test code right now, isn't possible write recursive method loop further down nodes?

something (untested code):

private static void handlenode(xmlnode node) {   if(node.haschildnodes)   {     foreach(xmlnode child in node.childnodes)     {       handlenode(child);     }   }     else     console.writeline(node.name); } 

then call method in place of console.wrintline()


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -