c# - Problem in reading XML node with unknown root/parent nodes -
i have been trying read xml file. have extract value of nodes "date" , "name", problem is, might appear @ level in xml hierarchy.
so when try code,
xmldocument doc = new xmldocument(); doc.load("test1.xml"); xmlelement root = doc.documentelement; xmlnodelist nodes = root.selectnodes("//*"); string date; string name; foreach (xmlnode node in nodes) { date = node["date"].innertext; name = node["name"].innertext; }
and xml file ::
<?xml version="1.0" encoding="utf-8"?> <root> <child> <name>aravind</name> <date>12/03/2000</date> </child> </root>
the above code errors out, <name>
, <date>
not immediate child elements of root.
possible assume parent/root nodes unknown , name of nodes, copy values ??
depending on exception getting, may or may not exact solution. however, check date
, name
exist before doing .innertext
on them.
foreach (xmlnode node in nodes) { datenode = node["date"]; if(datenode != null) date = datenode.innertext; // etc. }
Comments
Post a Comment