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?
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.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With