Grabbing specific node from 3rd party xml file

I am working off a 3rd party system that has provided me with an XML output, but no other documentation.

Using C#, I currently have string that is an XML file (see attached).  

I have no control or access to the XML schema or objects, as this is all 3rd party.

I would like to extract just the   <b:Body> part of the xml in to a separate string.  I want to use the correct XML functions to extract this.  I am currently using  string functions to get what I need, but am looking how to do this correctly.

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

DabasConnect With a Mentor Commented:
Since you say that you are using string functions to get what you need, I assume you have created a StreamReader to read the contents of the file into your string.

If that is the case, then you can use the method in the Code below

private void ExtractBody(StreamReader sr)
            XmlTextReader xr = new XmlTextReader(sr);
            while (xr.Read() && xr.Name != "b:Body")

Open in new window

using System;
using System.IO;
using System.Xml;

public class Sample
  public static void Main()

      XmlDocument doc = new XmlDocument();

      //Create an XmlNamespaceManager for resolving namespaces.
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("a", "");
      nsmgr.AddNamespace("b", "");

      //Select the book node with the matching attribute value.
      XmlNode book;
      XmlElement root = doc.DocumentElement;
      book = root.SelectSingleNode("//b:Body", nsmgr);


Bob LearnedCommented:
System.Xml.XmlDocument is good for 2.0 or lower, but I believe that we need to find out what version you are working with.  LINQ-to-XML with 3.5 and higher has the System.Linq.Xml.XDocument class, which is much easier to use than the XmlDocument class.
All Courses

From novice to tech pro — start learning today.