c# - Why is this program not accessing child nodes? -
here gets xml document , individual nodes, , inserts nodes dictionary.
//create xml document obj xmldocument inputxmldoc = new xmldocument(); fileref.isvalid = false; //load xml document #region try { inputxmldoc.xmlresolver = null; inputxmldoc.load( strfile );//load xml file string input = inputxmldoc.outerxml;//get string console.writeline( "success,loaded xml" ); logger.log( "loaded xml:" + strfile ); fileref.importlist = new dictionary<string, xmlnode>(); nodenames = new list<string> { "orderid", "customerid", "customername", "addresses", "orderstatus", "dateordered", "paymenttime", "includevat", "ordertotalincvat", "ordertotalvat", "currency", "typeofsaleid" }; try { int = 0; foreach( string name in nodenames ) { console.writeline( "adding xml node " + name ); if( inputxmldoc.getelementsbytagname( name ) != null ) { xmlnodelist xlist = inputxmldoc.getelementsbytagname( name ); foreach( xmlnode node in xlist ) { fileref.importlist.add( name, node ); //add individual node within nodelist console.writeline( name ); } } //add specified node xml doc else { nodenames.removeat( ); } i++; } } }
later, nodes accessed save information web service. however, nodes child nodes within not showing way.
invoices.address address = new invoices.address(); xmlnodelist onodelist = fileref.importlist["addresses"].selectnodes("/delivery/street"); foreach (xmlnode xn in onodelist) { address.street = xn.innertext; }
sample xml document
<?xml version="1.0" encoding="utf-8"?> <invoiceorder xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <orderid xmlns="http://24sevenoffice.com/webservices">35</orderid> <customerid xmlns="http://24sevenoffice.com/webservices">21</customerid> <customername xmlns="http://24sevenoffice.com/webservices">james bond</customername> <addresses xmlns="http://24sevenoffice.com/webservices"> <delivery> <street>11 shewell walk</street> <state /> <postalcode>co1 1wg</postalcode> <postalarea>essex</postalarea> <name /> <city>colchester</city> <country>uk</country> </delivery> <invoice> <street>10 shewell walk</street> <state /> <postalcode>co1 1wg</postalcode> <postalarea>essex</postalarea> <name /> <city>colchester</city> <country>uk</country> </invoice> </addresses> <orderstatus xmlns="http://24sevenoffice.com/webservices">offer</orderstatus> <dateordered xmlns="http://24sevenoffice.com/webservices">2015-06-15t14:00:00z</dateordered> <paymenttime xmlns="http://24sevenoffice.com/webservices">14</paymenttime> <includevat xsi:nil="true" xmlns="http://24sevenoffice.com/webservices" /> <ordertotalincvat xmlns="http://24sevenoffice.com/webservices">480.0000</ordertotalincvat> <ordertotalvat xmlns="http://24sevenoffice.com/webservices">80.0000</ordertotalvat> <currency xmlns="http://24sevenoffice.com/webservices"> <symbol>local</symbol> </currency> <typeofsaleid xmlns="http://24sevenoffice.com/webservices">-100</typeofsaleid> <invoicerows xmlns="http://24sevenoffice.com/webservices"> <invoicerow> <productid>18</productid> <rowid>4665754</rowid> <price>400.0000</price> <name>17" laptop screen</name> <discountrate>0.0000</discountrate> <quantity>7.0000</quantity> <cost>0.0000</cost> <inprice>0.0000</inprice> </invoicerow> </invoicerows> </invoiceorder>
the problem have namespaces. if specify namespace each of elements seems work. came conclusion bit of googling , experimentation explanation might not spot on advise researching issue further understand correctly.
this code work:
xmlnamespacemanager nsmgr = new xmlnamespacemanager(inputxmldoc.nametable); nsmgr.addnamespace("ns", "http://24sevenoffice.com/webservices"); var onodelist = importlist["addresses"].selectnodes("//ns:delivery/ns:street",nsmgr);
the reason (i think) in xml document specifying default namespace elements (xmlns="http://24sevenoffice.com/webservices"
) , in xpath not specifying same namespace. in code create namespace manager namespace in , prefix 2 elements considers match ones in document have these namespaces.
Comments
Post a Comment