user3771794
user3771794

Reputation:

Output everything between specified XML tag in C#

I need to get the everything between two Xml tags in C#, here is what i got so far{

XmlTextReader reader = new XmlTextReader(textBox1.Text);
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:
                Console.Write("<" + reader.Name);
                Console.WriteLine(">");
                break;
            case XmlNodeType.Text: //Display the text in each element.
                Console.WriteLine(reader.Value);
                break;
            case XmlNodeType.EndElement: //Display the end of the element.
                Console.Write("</" + reader.Name);
                Console.WriteLine(">");
                break;
        }
    }   
}

As you can see i have the file read in, and i can output all the tags to the console, how can i output everything between the tag i want to the console. I need to be able to go into many tags, like open

Upvotes: 1

Views: 162

Answers (1)

Paddy
Paddy

Reputation: 33867

I think you might have overcomplicated this slightly. It's probably just a matter of finding the node that you want and using the InnerXml property:

XmlDocument doc = new XmlDocument();

doc.LoadXml("<root>"+
            "<elem>test 1<child/>test 2</elem>" +
            "</root>");

XmlNode elem = doc.DocumentElement.FirstChild;

Console.WriteLine("Display the InnerXml of the element...");
Console.WriteLine(elem.InnerXml);

Upvotes: 2

Related Questions