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.