Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting an XML element from an XML file using XPath

Tags:

c#

xml

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?

like image 412
JL. Avatar asked Sep 18 '09 14:09

JL.


People also ask

How do you extract a specific XPath from a XML file?

xml file in a text editor. Define a new XPath text extractor. In the <AllFilters> element, add a <Filter> element for the new XPath text extractor. In the <Filter> element, add a <FilterName> element and specify the name of the text extractor.

Can we use XML in XPath?

XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.


2 Answers

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!

like image 184
Donut Avatar answered Sep 30 '22 02:09

Donut


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.

like image 37
Pete OHanlon Avatar answered Sep 30 '22 03:09

Pete OHanlon