xmlns 属性不会让我解析 [英] xmlns attribute won't let me parse

查看:23
本文介绍了xmlns 属性不会让我解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去一个小时我一直在尝试解析这个 XML 文件

I've been trying to parse this XML file for the last hour

<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>MEDO PUB</name>
    <SSIDConfig>
        <SSID>
            <hex>4D45444F20505542</hex>
            <name>MEDO PUB</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>manual</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>true</protected>
                <keyMaterial>someReallyLongString</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>

但我不断收到错误消息.这是一个保存的 Wi-Fi 配置文件,我使用 Managed Wifi API 导出 XML 文件.后来,我想从 XML 文件中解析和读取一些数据.我不能.认输后,我别无他法,只能修改XML文件.所以我尝试解析

but I kept getting errors. This is a saved Wi-Fi profile and I used Managed Wifi API to export the XML file. Later, I wanted to parse and read some data from the XML file. I couldn't. After admitting the defeat, I had nothing else to try but modify the XML file. So I tried parsing

<?xml version="1.0"?>
<WLANProfile>
    <name>MEDO PUB</name>
</WLANProfile>

它奏效了.xmlns="http://www.microsoft.com/networking/WLAN/profile/v1" 导致了问题.这是为什么?

and it worked. xmlns="http://www.microsoft.com/networking/WLAN/profile/v1" was causing the trouble. Why is that?

我将生成 XML 并动态读取它,因此我无法从 XML 文件中手动打开和删除该部分.我该如何解决这个问题?

I'll be generating the XML and reading from it on the fly, so I can't manually open and delete that part from the XML file. How can I fix this?

使用:

Visual C# 2010 Express(不是一体机,单独安装)

Visual C# 2010 Express (Not All-In-One, separate installation)

Windows 8.1 专业版 x64

Windows 8.1 Pro x64

XmlDocument doc = new XmlDocument();
doc.Load("c:/key.xml");
XmlNode node = doc.DocumentElement.SelectSingleNode("//WLANProfile/name");
XMLOutput.Text = node.InnerText;

推荐答案

xmlns="...."默认命名空间(无前缀命名空间声明).请注意,除非另有说明,否则后代元素隐式继承祖先默认命名空间.这意味着,在这个特定的 XML 中,所有元素都在默认命名空间中.

xmlns="...." is default namespace (unprefixed namespace declaration). Note that descendant elements inherit ancestor default namespace implicitly, unless otherwise specified. That means, in this particular XML, all elements are in the default namespace.

要使用 XPath 选择命名空间中的元素,您需要先注册指向相应命名空间的前缀,然后在 XPath 中正确使用注册的前缀:

To select element in namespace using XPath, you need to register prefix that point to the corresponding namespace first, then use the registered prefix properly in your XPath :

XmlDocument doc = new XmlDocument();
doc.Load("c:/key.xml");
var nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.Add("d", "http://www.microsoft.com/networking/WLAN/profile/v1");
XmlNode node = doc.DocumentElement.SelectSingleNode("//d:WLANProfile/d:name", nsManager);
XMLOutput.Text = node.InnerText;

这篇关于xmlns 属性不会让我解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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