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

筅森魡賤 提交于 2020-02-08 08:28:35

问题


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.


回答1:


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
}


来源:https://stackoverflow.com/questions/30095256/xmlreader-get-value-of-child-node-if-another-child-node-contains-a-special-stri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!