Reputation: 3874
I have an XML file from which I have to extract some specific nodes and put them in a SQL table.
I am using XmlReader and have switch case on XmlReader.Name
Here is just a sample one with very few nodes just for explanation.
<products>
<product>
<description>Nothing</description>
<cost>$34.78</cost>
<inventory>166</inventory>
</product>
<product>
<description>Nike Cap 17893</description>
<cost>$29.99</cost>
<inventory>54</inventory>
</product>
</products>
The idea is if there is Nothing in description node, I should ignore the entire product and move to the next product.
I wanted to use XmlReader.Skip() in that case but it seems it only skips the chidren nodes but the parents nodes.
Just wondering if C# provides any method to ignore parent node?
Upvotes: 0
Views: 595
Reputation: 2428
With Linq To Xml you could easily ignore all Elements that are "Nothing" and just process the other elements.
Here is quick Example.
XElement root = XElement.Load("file.xml");
IEnumerable<XElement> productsWithoutNothing = from product in root.Elements("product")
where (string)product.Element("description") != "Nothing"
select product;
Upvotes: 1
Reputation: 1
I think it would be much easier if you use XSL to filter the nodes.
Step 1: Use XSL to get a list of (parent) nodes which has description
Step 2: Loop through this filtered set
Upvotes: 0