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.

Thanks!
blobXMLemp.xml
schaeff2Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
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

Dabas
private void ExtractBody(StreamReader sr)
        {
            XmlTextReader xr = new XmlTextReader(sr);
            while (xr.Read() && xr.Name != "b:Body")
                Debug.WriteLine(xr.Name);
            Debug.WriteLine(xr.ReadElementContentAsString());
        }

Open in new window

0
 
apeterCommented:
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

      XmlDocument doc = new XmlDocument();
      doc.Load("booksort.xml");

      //Create an XmlNamespaceManager for resolving namespaces.
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("a", "http://schemas.apott.com/2008/01/DataContracts/ContentItem");
      nsmgr.AddNamespace("b", "http://schemas.apott.com/2008/01/DataContracts/ContentHtml");

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

      Console.WriteLine(book.OuterXml);

  }
}
0
 
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.
0
All Courses

From novice to tech pro — start learning today.