简体   繁体   English

使用XPath从XML文件中提取XML元素

[英]Extracting an XML element from an XML file using XPath

I have the following XML document: 我有以下XML文档:

  <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. 整个文件包含约700个条目。 How can I extract a single MimeType element using XPath and populate it into a strongly typed C# MimeType object? 如何使用XPath提取单个MimeType元素并将其填充到强类型的C# MimeType对象中?

Use XmlDocument.SelectSingleNode . 使用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): 然后,您可以访问node.ChildNodes以获取所需的值(示例):

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

And then use those values when constructing your MimeType object. 然后在构造MimeType对象时使用这些值。

Edit: Some XPath info. 编辑:一些XPath信息。
There are some really good XPath tutorials out there, try here and here . 有一些非常好的XPath教程,请尝试这里这里 The W3C recommendation itself can be a bit overwhelming. W3C推荐本身可能有点压倒性。
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): 对于您的示例,您可以尝试使用以下XPath来选择文档中的第一个MimeType节点(其中root是您的根元素的名称):

string xPath = "root/MimeType[1]"

Hope that helps! 希望有所帮助!

The following method should provide a framework for getting the MIME Type here 以下方法应提供在此处获取MIME类型的框架

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. 诀窍是根据扩展中的文本找到MimeType,然后为此匹配检索MimeType的祖先。

You use the classes in the System.Xml.XPath namespace. 您使用System.Xml.XPath命名空间中的类。

Tutorials from MSDN 来自MSDN的教程

Following up on the answer by @MiffTheFox , you can use XPath together with LINQ to XML . 按照@MiffTheFox的回答 ,您可以将XPathLINQ to XML一起使用。 Here's an example based on your sample data. 这是基于您的示例数据的示例。

1) First, the namespaces you'll need: 1)首先,您需要的名称空间:

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

2) Load your XML document into an XElement : 2)将XML文档加载到XElement

XElement rootElement = XElement.Parse(xml);

3) Define your XPath location path: 3)定义XPath位置路径:

// 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: 4)将位置路径传递给XPathSelectElement()以选择感兴趣的元素:

XElement selectedElement = rootElement.XPathSelectElement(locationPath);

5) Finally, extract the MimeType value that is associated with the extension: 5)最后,提取与扩展名相关联的MimeType值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM