简体   繁体   English

从文档中选择单个 XML 节点

[英]Select single XML node from document

I'm trying to load some values from an XML document returned by a Philips Hue Bridge.我正在尝试从飞利浦 Hue Bridge 返回的 XML 文档中加载一些值。 Here is an example of the XML I'm trying to parse (example taken from Philips' website):这是我尝试解析的 XML 示例(示例取自飞利浦网站):

<root xmlns="urn:schemas-upnp-org:device-1-0">
    <specVersion>
        <major>1</major>
        <minor>0</minor>
    </specVersion>
    <URLBase>https://192.168.1.130:80/</URLBase>
    <device>
        <deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>
        <friendlyName>Philips hue (192.168.1.130)</friendlyName>
        <manufacturer>Royal Philips Electronics</manufacturer><manufacturerURL>https://www.philips.com</manufacturerURL>
        <modelDescription>Philips hue Personal Wireless Lighting</modelDescription>
        <modelName>Philips hue bridge 2012</modelName>
        <modelNumber>929000226503</modelNumber>
        <modelURL>https://www.meethue.com</modelURL>
        <serialNumber>001788102201</serialNumber>
        <UDN>uuid:2f402f80-da50-11e1-9b23-001788102201</UDN>
        <presentationURL>index.html</presentationURL>
        <iconList>
            <icon>
                <mimetype>image/png</mimetype>
                <height>48</height>
                <width>48</width>
                <depth>24</depth>
                <url>hue_logo_0.png</url>
            </icon>
            <icon>
                <mimetype>image/png</mimetype>
                <height>120</height>
                <width>120</width>
                <depth>24</depth>
                <url>hue_logo_3.png</url>
            </icon>
        </iconList>
    </device>
</root>

I've looked at several examples online and tried various paths, but the result is always null.我在网上查看了几个示例并尝试了各种路径,但结果始终为空。 I've put multiple examples in comments to show you some of the things I've tried.我在评论中放了多个例子来向你展示我尝试过的一些事情。

var xml = webClient.DownloadString($"http://{bridge.internalipaddress.ToString()}/description.xml");
XmlDocument bridgeDetails = new XmlDocument();
bridgeDetails.LoadXml(xml);
XmlNode root = bridgeDetails.DocumentElement;
var friendlyName = bridgeDetails.SelectSingleNode("//device/friendlyName");
//var friendlyName = bridgeDetails.SelectSingleNode("/device/friendlyName");
//var friendlyName = bridgeDetails.SelectSingleNode("device/friendlyName");
//var friendlyName = bridgeDetails.SelectSingleNode("descendant::device/friendlyName");
//var friendlyName = root.SelectSingleNode("//device/friendlyName");
//var friendlyName = root.SelectSingleNode("/device/friendlyName");
//var friendlyName = root.SelectSingleNode("device/friendlyName");
//var friendlyName = root.SelectSingleNode("descendant::device/friendlyName");
//I've also tried adding .Value.ToString() at the end of some of these, but that just caused a null reference exception

EDIT:编辑:

I've worked out that I'm missing the namespace.我已经发现我缺少命名空间。 But I'm not sure how as there is no URL to the namespace.但我不确定如何,因为没有命名空间的 URL。 Eg例如

var nsmgr = new XmlNamespaceManager(bridgeDetails.NameTable);
nsmgr.AddNamespace("upnp", "urn:schemas-upnp-org:device-1-0");

XmlNode root = bridgeDetails.DocumentElement;
var friendlyName = bridgeDetails.SelectSingleNode("//upnp:device/friendlyName");

Any help would be appreciated.任何帮助,将不胜感激。

Your solution after adding the namespace was close.您添加命名空间后的解决方案已关闭。
Now use the following XPath-1.0 expression, because all children or <root> are in this namespace:现在使用以下 XPath-1.0 表达式,因为所有子项或<root>都在此命名空间中:

//upnp:device/upnp:friendlyName

In complete, this should look like完整的,这应该看起来像

var friendlyName = bridgeDetails.SelectSingleNode("//upnp:device/upnp:friendlyName", nsmgr);

and return you the value并返回您的价值

Philips hue (192.168.1.130)飞利浦色调 (192.168.1.130)


Relating to your statement关于你的陈述

But I'm not sure how as there is no URL to the namespace.但我不确定如何,因为没有命名空间的 URL。

The namespace URI does not have to be a valid URL.命名空间 URI 不必是有效的 URL。 It just has to be a unique string (in your "name"-space ) and it doesn't have to point to a retrievable file.它只需要是一个唯一的字符串(在您的“名称”-space 中),并且不必指向可检索的文件。 That's not neccessary.那不是必须的。

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

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