C# Read XML Multiple Nested -


i have raw xml file generated report. format of xml extremely convoluted. goal of trying go through xml , value of objectname , formattedvalue inside each node of <formattedareapair level="2" type="details">:

<formattedreport xmlns="urn:crystal-reports:schemas"                   xmlns:xsi="http://www.w3.org/2000/10/xmlschema-instance">   <formattedareapair level="0" type="report">     <formattedareapair level="1" type="group">       <formattedareapair level="2" type="details">         <formattedarea type="details">           <formattedsections>             <formattedsection sectionnumber="0">               <formattedreportobjects>                 <formattedreportobject xsi:type="ctformattedfield"                                        type="xsd:string"                                         fieldname="{ado.agent_extension}">                   <objectname>field2</objectname>                    <formattedvalue>3531</formattedvalue>                    <value>3531</value>                  </formattedreportobject>                 <formattedreportobject xsi:type="ctformattedfield"                                        type="xsd:string"                                         fieldname="{ado.agent_state}">                   <objectname>field4</objectname>                    <formattedvalue>logged-in</formattedvalue>                    <value>logged-in</value>                  </formattedreportobject>                 <formattedreportobject xsi:type="ctformattedfield"                                        type="xsd:short"                                         fieldname="{ado.reason_code}">                   <objectname>field5</objectname>                    <formattedvalue>0</formattedvalue>                    <value>0.00</value>                  </formattedreportobject>                 <formattedreportobject xsi:type="ctformattedfield"                                        type="xsd:string"                                         fieldname="{@tf_duration}">                   <objectname>field7</objectname>                    <formattedvalue>0:00:00</formattedvalue>                    <value>0:00:00</value>                  </formattedreportobject>                 <formattedreportobject xsi:type="ctformattedfield"                                        type="xsd:string"                                         fieldname="{@tf_agent_id}">                   <objectname>templatefield11</objectname>                    <formattedvalue>users_name</formattedvalue>                    <value>users_name</value>                  </formattedreportobject>                 <formattedreportobject xsi:type="ctformattedfield"                                        type="xsd:timeinstant"                                         fieldname="{@tf_transition_time}">                   <objectname>templatefield21</objectname>                    <formattedvalue>6/1/2015 6:43:31am</formattedvalue>                    <value>2015-06-01t06:43:31</value>                  </formattedreportobject>               </formattedreportobjects>             </formattedsection>           </formattedsections>         </formattedarea>       </formattedareapair> 

i have tried several different ways (i quite noob reading xml).

using system.xml: - no data found

xmldocument xmldoc = new xmldocument(); xmldoc.load(@"c:\test\reports\test.xml"); xmlnode node = xmldoc.documentelement.firstchild; xmlnodelist lstfields = node.childnodes;  for(int = 0; <lstfields.count; i++) {     //look node     if (lstfields[i].name == "formattedsections")     {         xmlnodelist lstcrap = lstfields[i].childnodes;         (int j = 0; j < lstcrap.count; j++)         {             txttest.text += lstcrap[j].innertext + "\n";         }     } } 

using system.xml.linq:

private string pullvalue (string productid)     {         xdocument xdoc = xdocument.load(@"c:\test\reports\test.xml");         var detailstest = xdoc             .descendants("formattedreportobjects")             .where(extension => extension.descendants("formattedreportobject")             .any(number => (string)number.attribute("value")             == productid)).firstordefault();         return (string)detailstest;     } 

what problem? it's simple!

xelement report = xelement.load("file.xml");  xnamespace ns = "urn:crystal-reports:schemas";  var formattedareapair = report     .descendants(ns + "formattedareapair")     .where(elem => elem.attribute("level").value == "2" && elem.attribute("type").value == "details")     .first();  foreach (var elem in formattedareapair.descendants(ns + "formattedreportobject")) {     console.writeline(elem.element(ns + "objectname").value);     console.writeline(elem.element(ns + "formattedvalue").value);     console.writeline(); } 

Comments