JL.
JL.

Reputation: 81262

Extracting an XML element from an XML file using XPath

I have the following XML document:

  <MimeType>
     <Extension>.aab</Extension>
     <Value>application/x-authorware-</Value>
  </MimeType>
  <MimeType>
     <Extension>.aam</Extension>
     <Value>application/x-authorware-</Value>
  </MimeType>

The whole document contains about 700 entries. How can I extract a single MimeType element using XPath and populate it into a strongly typed C# MimeType object?

Upvotes: 8

Views: 26964

Answers (4)

DavidRR
DavidRR

Reputation: 19397

Following up on the answer by @MiffTheFox, you can use XPath together with LINQ to XML. Here's an example based on your sample data.

1) First, the namespaces you'll need:

using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

2) Load your XML document into an XElement:

XElement rootElement = XElement.Parse(xml);

3) Define your XPath location path:

// For example, to locate the 'MimeType' element whose 'Extension'
// child element has the value '.aam':
//
//     ./MimeType[Extension='.aam']

string extension = ".aam";
string locationPath = String.Format("./MimeType[Extension='{0}']", extension);

4) Pass the location path to XPathSelectElement() to select the element of interest:

XElement selectedElement = rootElement.XPathSelectElement(locationPath);

5) Finally, extract the MimeType value that is associated with the extension:

var mimeType = (string)selectedElement.Element("Value");

Upvotes: 2

Donut
Donut

Reputation: 112815

Use XmlDocument.SelectSingleNode.

Example:

XmlDocument doc = new XmlDocument();
doc.Load("yourXmlFileName");
XmlNode node = doc.SelectSingleNode("yourXpath");

Then you can access node.ChildNodes in order to get the values you want (example):

string extension = node.ChildNodes[0].InnerText;
string value = node.ChildNodes[1].InnerText;

And then use those values when constructing your MimeType object.

Edit: Some XPath info.
There are some really good XPath tutorials out there, try here and here. The W3C recommendation itself can be a bit overwhelming.
For your example, you could try using the following XPath to select the first MimeType node in the document (where root is whatever the name of your root element):

string xPath = "root/MimeType[1]"

Hope that helps!

Upvotes: 15

Pete OHanlon
Pete OHanlon

Reputation: 9146

The following method should provide a framework for getting the MIME Type here

public MimeType RunXPath(string mimeType)
{
  XmlNode node = _xmlDoc.SelectSingleNode(
    string.Format("//MimeType/Extension[text()="{0}"]/ancestor::MimeType", mimeType));
  foreach(XmlNode node in nodes)
  {
    // Extract the relevant nodes and populate the Mime Type here...
  }

  return ...
}

The trick is to find the MimeType based on the text in the extension, then retrieve the ancestor of MimeType for this match.

Upvotes: 2

MiffTheFox
MiffTheFox

Reputation: 21565

You use the classes in the System.Xml.XPath namespace.

Tutorials from MSDN

Upvotes: 1

Related Questions