c# - Read xml from file -
i got little problem reading information xml file...
the file passed me has thousands of lines. im interested in 300 - 400 of lines. don't need write data xml when user finished operation , data read can stored in list<string>
.
i found solutions on net using xmltextreader
read data. wouldnt have create class , use serializer. seems im using xmltextreader
wrong. maybe can me...
this how xml looks like:
<?xml version="1.0" encoding="utf-8"?> <projectconfiguration xmlns:xsd="http://www.w3.org/2001/xmlschema" xsi="http://www.w3.org/2001/xmlschema-instance"> <projectlists xmlns="..."> <projectlist> ... // not interested in data </projectlist> <projectlist> <listnode> <name>test_environment</name> <children> <listnode> <name>yyy</name> <children> <listnode> <name>205 (st)</name> <children> <listnode> <name>098-0031</name> <children /> </listnode> <listnode> <name>098-0032</name> <children /> </listnode> //more listnodes... </children> </listnode> <listnode> <name>old</name> <children> <listnode> <name>w098-32</name> <children /> </listnode> </children> </listnode> </children> </listnode> <listnode> <name>xxx</name> <children /> </listnode> <listnode> <name>098-0001</name> <children /> </listnode> <listnode> <name>098-0011</name> <children /> </listnode> // more list nodes </children> </listnode> <listnode> // more list nodes </listnode> </projectlist> <projectlist> //more uninteresting projectlists... </projectlist>
i'm interested in value
of inner name elements (the first 2 "098-0031" , "098-0032").
and code:
while (reader.read()) { switch (reader.nodetype) { case xmlnodetype.element: { if (reader.name == "name") { reader.read(); if (reader.value == "test_environment") { reader.readtodescendant("children"); if (reader.name == "children") { reader.readtodescendant("children"); } } } } break; } }
but condition reader.name == "children"
never fullfilled... can explain me why. , maybe show me easy way store values in list<string>
? in advance!
edit: edited xml. sorry hard filter unnecassary confusing parts xml...
static void getmostinnername() { string xml = @"<projectconfiguration xmlns:xsd=""http://www.w3.org/2001/xmlschema"" xsi=""http://www.w3.org/2001/xmlschema-instance""> <projectlists> <projectlist> <listnode> <name>test_environment</name> <children> <listnode> <name>yyy</name> <children> <listnode> <name>205 (st)</name> <children> <listnode> <name>098-0031</name> <children /> </listnode> <listnode> <name>098-0032</name> <children /> </listnode> </children> </listnode> <listnode> <name>old</name> <children> <listnode> <name>w098-32</name> <children /> </listnode> </children> </listnode> </children> </listnode> <listnode> <name>xxx</name> <children> <listnode> <name>098-0001</name> <children /> </listnode> <listnode> <name>098-0011</name> <children /> </listnode> </children> </listnode> // more list nodes </children> </listnode> </projectlist></projectlists> </projectconfiguration>"; xelement root = xelement.parse(xml).element("projectlists"); //var xmlns = root.getdefaultnamespace(); //console.writeline(xmlns); var eles = root.elements("projectlist").selectmany(x => x.elements("listnode")); list<string> list = new list<string>(); foreach (var ele in eles) { loop(ele, list); } list.foreach(x => { console.writeline(x); }); } static void loop(xelement ele, list<string> list) { var child = ele.element("children"); if (child != null && child.haselements) { foreach (var e in child.elements("listnode")) { loop(e, list); } } else { list.add(ele.element("name").value); } }
because xml has many node projectlist
,so used selectmany
here,and add root
element have test,the last output is
098-0031 098-0032 w098-32 098-0001 098-0011
Comments
Post a Comment