I am parsing an xml string using xmlreader in c#, but as i parse i need sometimes to get the full content of a node including children with tags and still be able to continue parsing.
ReadInnerXML and ReadOutterXML break up everything for me
example XML:
<?xml version="1.0" standalone="yes"?>
<main>
<parse1> //finding this will get full inner or outter xml - either one - my issue
<parse2 /> // even getting the children above might still need parse chldren
<negligeable /> // not all children interest me
</parse1>
<parse3>some text</parse3> // not all children of main are the same but all need be parsed
</main>
Hope this gives you a general idea of what i need
i can parse 2 and 3 right now and ignore what i don't need but if i use ReadInnerXML or ReadOutterXML when i find the tag then it won't let me parse anything else - not even the tag wich is outside .
ReadInnerXML and ReadOutterXML do return the text i need correctly but cause everything else to not be parsed
EDIT: as per dasblinkenlight sugestion, some code:
using (XmlReader reader = XmlReader.Create(new StringReader(XmlString)))
{
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
switch (reader.Name)
{
case "parse1":
Console.WriteLine("Contents of Parse 1: {0}", ?function here?);
break;
case "parse2":
Console.WriteLine("Parse 2 tag exists");
break;
case "parse3":
Console.WriteLine("Contents of Parse 3: {0}", Reader.ReadElementContentAsString());
break;
}
break;
}
}
}
Result should be (given the test xml)
Contents of Parse 1: <parse2 /><negligeable />
Parse 2 tag exists
Contents of Parse 3: some text
Am also trying ReadSubTree
Any hints?
Basically readinnerxml is reading all the way to the end and XmlReader is forward only. You might get away with XmlDocument, or another way, would be to create another reader from the same Xml content, read to where you are in the orginal, get your string and bin the 'copy'