c# - Retrieving a Deeply Nested Value in an XML File -


i'm trying read property in xml file using c# , linq xml , can't retrieve value nested in tree. value i'm trying contents of <value> near <displayname>add comments</displayname>. every <orderproduct id=???> may have own comments.

i can read other properties in xml file using linq, i'm confused how go reading nested.

thanks.

<?xml version="1.0" encoding="utf-16"?> <orderxml>   <order>     <orderproducts>       <orderproduct id="1">       .       .       .       </orderproduct>        <orderproduct id="2">         <propertyvalues>           <propertyvalue>             <property id="10786">               <displayname>base</displayname>             </property>             <value />           </propertyvalue>            <propertyvalue>             <property id="10846">               <displayname>add comments</displayname>             </property>             <value>this comment</value>           </propertyvalue>         </propertyvalues>       </orderproduct>     </orderproducts>   </order> </orderxml> 

this code have far. can retrieve "add comments" part, i'm stuck in how part follows it.

string productorderid = "";  string productname = "";  xelement xelement; xelement = xelement.load (@"d:\order.xml");  ienumerable<xelement> products = xelement.descendants ("orderproduct");  foreach (var order in products) {   productorderid = order.attribute ("id").value;   productname = order.element ("product").element ("name").value;    console.writeline ("productorderid: {0}", productorderid);   console.writeline ("productname: {0}", productname);   console.writeline ("");    ienumerable<xelement> propertyvalues = xelement.descendants ("propertyvalues").elements ("propertyvalue");    foreach (var propvalue in propertyvalues.elements ("property").elements ("displayname"))   {     console.writeline ("property id: {0}", propvalue.value);      if (propvalue.value == "add comments")     {       console.writeline ("---");     }   } } 

you can use descendants search nodes in document no matter are:

string name = "add comments"; var value = xdoc    .descendants("propertyvalue")    .where(pv => (string)pv.element("property").element("displayname") == name)    .select(pv => (string)pv.element("value"))    .firstordefault(); 

output:

this comment 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -