parsing a xml file which contains more than one keyvalues using c# -


i have file format

<?xml version='1.0' encoding='us-ascii'?> <root>   <file id="001">     <filename>abc.wav</filename>     <value>0.18</value>   </file>   <file id="002">     <filename>efg.wav</filename>     <value>0.05</value>     <value>0.14</value>   </file> </root> 

i want parse using c#

doc.load(confidencethresholdfilepath+"\\model.xml"); xmlnodelist nodes = doc.documentelement.selectnodes("/root/file");  list<result> results = new list<result>();  foreach (xmlnode node in nodes) {     result result = new result();     result.asfilename= node.selectsinglenode("filename").innertext;     result.resultedseconds = node.selectsinglenode("value").innertext;     results.add(result); } 

it gives result misses second record's second value.how results without fail.

how using linq xml?

var xdoc = xdocument.load("input.xml");  var results =     xdoc.root         .elements("file")         .select(f => new         {             filename = (string)f.element("filename"),             values = f.elements("value").select(v => (string)v).tolist()         })         .tolist(); 

results list of anonymous type instances 2 properties: filename:string , values:list<string>. can change return list<record> instead, change f => new f => new record , update properties info.

as can see, it's easier xml content using linq xml using old-style xmlsomething classes.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -