Search code examples
c#xmlxmlreader

XMLReader: Get value of child node if another child node contains a special string


I have a XML structure (only a snippet) as follows:

<ttOutput>
    <ttOutputRow>
       <cKey>key</cKey>
       <cValue>value</cValue>
    </ttOutputRow>
    <ttOutputRow>
       <cKey>findMe</cKey>
       <cValue>value</cValue>
    </ttOutputRow>
    <ttOutputRow>
       <cKey>key</cKey>
       <cValue>value</cValue>
     </ttOutputRow>
</ttOutput>

For this I want to use XMLReader to keep memory low. I'm looking for cValue if cKey is findMe. How can I get this value?

This is what I tried:

using (var stream = new StreamReader(path))
using (XmlReader reader = XmlReader.Create(stream))
{
    while(reader.Read())
    {
        if (reader.IsStartElement())
        {
            Console.WriteLine (reader.Name);
            if (reader.Name == "ttOutputRow") {
                XmlReader subreader = reader.ReadSubtree ();
                while (subreader.Read ()) {
                    if (subreader.Name == "cKey" && subreader.Value == "findMe") {
                        subreader.ReadToNextSibling ("cValue");
                        Console.WriteLine ("found");
                    }
                }
            }
        }
    }
}

Perhaps I can use ReadToDescendant(String) or ReadSubtree() here, but I don't know how to use it correctly.

Edit:

This seems to be another option using ReadToDescendant:

while(reader.Read())
{
    reader.ReadToFollowing ("ttOutputRow");
    do {
        if (reader.ReadToDescendant ("cKey")) {
            if (reader.ReadInnerXml() == "findMe") {
                reader.ReadToNextSibling ("cValue");
                Console.WriteLine ("found");
            }
        }
    } while (reader.ReadToFollowing ("ttOutputRow"));
}

I also found out that you can only use ReadInnerXml() one time.


Solution

  • Use subreader.Value when the reader points to the child text node instead of pointing to the parent element node. You can also use ReadInnerXml() to get text value of an element node provided that the element doesn't have child element, for example :

    if (subreader.Name == "cKey" && subreader.ReadInnerXml() == "findMe") 
    {
        subreader.ReadToNextSibling("cValue");
        Console.WriteLine ("found: {0}", subreader.ReadInnerXml());
        //output:
        //found: value
    }