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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.