XPath文本提取 [英] XPath Text Extraction

查看:115
本文介绍了XPath文本提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个XML文件,我需要提取一些内容,但是文件是非标准的XML而且我不熟悉用XPath。我曾尝试在互联网上进行研究,但所提出的解决方案似乎不起作用。



基本上文件的XML内容如下:



Hi,

I have an XML file, and I need to extract some of its contents, however the file is non-standard XML and I am not familiar with XPath. I have tried researching on the internet however the presented solutions did not seem to work.

Basically the XML content of the file is as follows:

<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<URLBase>http://192.168.1.254:8000</URLBase>
<device>
<deviceType>
urn:schemas-upnp-org:device:InternetGatewayDevice:1
</deviceType>
<friendlyName>Name and Version here...</friendlyName>
<manufacturer>Name here...</manufacturer>
<manufacturerURL>Website here...</manufacturerURL>
<modelDescription>Internet Gateway Device</modelDescription>
<modelName>Model here...</modelName>
<modelNumber>Number here...</modelNumber>
<modelURL>URL here...</modelURL>
<serialNumber>Serial Number here...</serialNumber>
<UDN>uuid:UPnP_Name and MAC here...</UDN>
<presentationURL>/</presentationURL>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:Layer3Forwarding:1</serviceType>
<serviceId>urn:upnp-org:serviceId:L3Forwarding1</serviceId>
<controlURL>/IGD/upnp/control/igd/layer3f</controlURL>
<eventSubURL>/IGD/upnp/event/igd/layer3f</eventSubURL>
<SCPDURL>/IGD/upnp/Layer3Forwarding.xml</SCPDURL>
</service>
</serviceList>
</device>
</root>





我需要提取 deviceType中的文字 serviceType 标签。我已经尝试使用/ deviceType标签,但它没有提取任何内容,而// *似乎检索文档中的所有元素,但这不是我想到的。很抱歉打扰可能是一个非常简单的问题,但我不能让它工作,我不知道哪个方面我做错了。非常感谢此文本提取的任何代码片段。



P.S.在原始XML文档中,有多个 deviceType serviceType 标记,我需要将它们全部提取出来,而不仅仅是第一次出现。谢谢。



I need to extract the text in the deviceType and serviceType tags. I have tried using the "/deviceType" tag but it extracts nothing, while the "//*" seems to retrieve all the elements in the document, however that is not what I had in mind. Sorry to be bothering with possibly a very easy question, but I can''t get it to work and I''m not sure which aspect I''m doing wrong. Any code snippets of this text extraction are greatly appreciated.

P.S. In the original XML document, there are multiple deviceType and serviceType tags and I need to extract them all, not just the first occurrence. Thanks.

推荐答案

您必须在代码中处理命名空间,例如

You have to handle the namespace in your code, e.g.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("x", "urn:schemas-upnp-org:device-1-0");
      string deviceType = doc.DocumentElement.SelectSingleNode("./x:device/x:deviceType", nsmgr).InnerText;





请注意 XML 您发布的文件无效:在结尾附近有一个虚假的deviceType结束标记:





Please note the XML document you posted in invalid: there is a spurious deviceType closing tag near the end:

</serviceList>
</deviceType> <==== ERROR HERE
</device>
</root>


简单示例来自 here [ ^ ] ...
Simple example is given over here[^]...


这篇关于XPath文本提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆