0

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?

5
  • 4
    how big is this xml file ? can't you use Linq to XML ? Commented Jul 25, 2014 at 14:26
  • The XML file will typically have 10,000 to 12,000 products Commented Jul 25, 2014 at 14:28
  • 1
    @bluepiranha: That still sounds like you should easily be able to load it into memory, which would make it much easier to handle than with XmlReader. Commented Jul 25, 2014 at 14:28
  • @JonSkeet Pardon my noviceness, but when you say load it into memory, what C# API class do you suggest me to read the XML file? Commented Jul 25, 2014 at 14:30
  • 4
    Use LINQ to XML, as suggested by Selman22, e.g. XDocument doc = XDocument.Load("products.xml") Commented Jul 25, 2014 at 14:31

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.