c# - Read Node attributes from XML file -


i have following xml file.

<?xml version="1.0" encoding="utf-8" ?> <countries>     <country>         <id>101</id>         <city>austin</city>     </country>     <country>         <id>102</id>         <city>dallas</city>     </country>     <country>         <id>103</id>         <city>chicago</city>     </country>     <country>         <id>104</id>         <city>aman</city>      </country> </countries> 

i trying read code behind. here code

list<city> citylist = new list<city>(); string url = "/app_data/countries.xml"; xmldocument doc = new xmldocument(); doc.load(server.mappath(url)); xmlnodelist nodelist = doc.selectnodes("country"); foreach (xmlnode node in nodelist) {     if (node != null)     {                                 int id = int.parse(node.attributes["country"].value);         string name = node.attributes["city"].value;         city city = new city(id, name);         citylist.add(city);     } } 

for reason, code below

xmlnodelist nodelist = doc.selectnodes("country"); 

returns no nodes @ all.

if targeting .net 3.5 or upper, consider using xdocument api instead of xmldocument api (see xdocument or xmldocument).


for case, implementation this:

var citylist = new list<city>();             xdocument xdoc = xdocument.load(server.mappath("/app_data/countries.xml")); foreach (xelement xcountry in xdoc.root.elements()) {     int id = int.parse(xcountry.element("id").value);     string name = xcountry.element("city").value;                     citylist.add(new city(id, name)); } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -