I have an XML file as shown below:
<clients> <client> <id>YYYY</id> <name>XXXX</name> <desc>ZZZZ</desc> <trade_info> <tab_list> <data> <tab>book 123</tab> </data> <data> <tab>cook 321</tab> </data> </tab_list> <buy_price_rate>200</buy_price_rate> </trade_info> </client> </clients>
I need to extract id, name, desc from it and from internal node trade_info I need data/tab, buy_price_rate .
So, initially I thought about this:
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(); }
But it does not work, and I'm not sure how to debug it.
From the first error I received, Null Expection, I believe that it could come from node.Descendants("trade_info") , as some clients do not have trade_info at all.
I also believe that there are some of them:
Id = n.Element("tab_list").Element("data").Element("tab").Value, Buy = n.Element("buy_price_rate").Value
As sometimes they do not have items in the list or buy_price_rate.
- How to check in my request is valid or not to protect it.
- Is my request approved for what I want?
- What should i change? Tips?
source share