Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing XML with inner nodes?
    primarykey
    data
    text
    <p>I have a XML file like the below:</p> <pre><code> &lt;clients&gt; &lt;client&gt; &lt;id&gt;YYYY&lt;/id&gt; &lt;name&gt;XXXX&lt;/name&gt; &lt;desc&gt;ZZZZ&lt;/desc&gt; &lt;trade_info&gt; &lt;tab_list&gt; &lt;data&gt; &lt;tab&gt;book 123&lt;/tab&gt; &lt;/data&gt; &lt;data&gt; &lt;tab&gt;cook 321&lt;/tab&gt; &lt;/data&gt; &lt;/tab_list&gt; &lt;buy_price_rate&gt;200&lt;/buy_price_rate&gt; &lt;/trade_info&gt; &lt;/client&gt; &lt;/clients&gt; </code></pre> <p>I need to extract from it <code>id, name, desc</code> and from the inner node trade_info I need <code>data/tab, buy_price_rate</code>.</p> <p>So initially I was thinking of this:</p> <pre><code> var query = from node in doc.Descendants("client") select new { client = new { Id = node.Element("id").Value, Name = node.Element("name").Value, Desc = node.Element("desc").Value }, trade = from n in node.Descendants("trade_info") select new { Id = n.Element("tab_list").Element("data").Element("tab").Value, Buy = n.Element("buy_price_rate").Value } }; foreach (var item in query) { writeXML.WriteStartElement("tradelist_template"); writeXML.WriteAttributeString("client_id", item.client.Id); foreach (var trade in item.trade) { writeXML.WriteStartElement("tradelist"); writeXML.WriteAttributeString("item_id", trade.Id); writeXML.WriteEndElement(); } writeXML.WriteEndElement(); } </code></pre> <p>But it does not seem to work and im not sure on how to debug it.</p> <p>From the first error I got, Null Expection I belive it may be coming from the <code>node.Descendants("trade_info")</code> as some clients have no trade_info at all.</p> <p>I also belive there is some comming from:</p> <pre><code>Id = n.Element("tab_list").Element("data").Element("tab").Value, Buy = n.Element("buy_price_rate").Value </code></pre> <p>As sometimes they dont have items on the list or buy_price_rate.</p> <ul> <li>How do I check within my query wether it is null or not to secure it</li> <li>Is my query ok for what I want ?</li> <li>What should I change ? Advices ?</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload