c# - 如何从以下文件中读取 xml 元素?

标签 c# xml

我有以下 xml 文件,我正在尝试读取我无法读取的 name 元素,知道如何读取该元素和其他元素吗?

<?xml version="1.0" encoding="us-ascii"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>tata</name>
    <SSIDConfig>
        <SSID>
            <name>SampleSingleSignOn</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <autoSwitch>false</autoSwitch>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2</authentication>
                <encryption>AES</encryption>
                <useOneX>true</useOneX>
            </authEncryption>
            <OneX xmlns="http://www.microsoft.com/networking/OneX/v1">
                <cacheUserData>true</cacheUserData>
                <maxAuthFailures>3</maxAuthFailures>
                <authMode>user</authMode>
                <singleSignOn>
                    <type>preLogon</type>
                    <maxDelay>10</maxDelay>
                </singleSignOn>
                <EAPConfig>
                    <EapHostConfig xmlns="http://www.microsoft.com/provisioning/EapHostConfig"
                                        xmlns:eapCommon="http://www.microsoft.com/provisioning/EapCommon"
                                        xmlns:baseEap="http://www.microsoft.com/provisioning/BaseEapMethodConfig">
                        <EapMethod>
                            <eapCommon:Type>25</eapCommon:Type>
                            <eapCommon:AuthorId>0</eapCommon:AuthorId>
                        </EapMethod>
                        <Config xmlns:baseEap="http://www.microsoft.com/provisioning/BaseEapConnectionPropertiesV1"
                                  xmlns:msPeap="http://www.microsoft.com/provisioning/MsPeapConnectionPropertiesV1"
                                  xmlns:msChapV2="http://www.microsoft.com/provisioning/MsChapV2ConnectionPropertiesV1">
                            <baseEap:Eap>
                                <baseEap:Type>25</baseEap:Type>
                                <msPeap:EapType>
                                    <msPeap:ServerValidation>
                                        <msPeap:DisableUserPromptForServerValidation>false</msPeap:DisableUserPromptForServerValidation>
                                        <msPeap:TrustedRootCA />
                                    </msPeap:ServerValidation>
                                    <msPeap:FastReconnect>true</msPeap:FastReconnect>
                                    <msPeap:InnerEapOptional>0</msPeap:InnerEapOptional>
                                    <baseEap:Eap>
                                        <baseEap:Type>26</baseEap:Type>
                                        <msChapV2:EapType>
                                            <msChapV2:UseWinLogonCredentials>true</msChapV2:UseWinLogonCredentials>
                                        </msChapV2:EapType>
                                    </baseEap:Eap>
                                    <msPeap:EnableQuarantineChecks>false</msPeap:EnableQuarantineChecks>
                                    <msPeap:RequireCryptoBinding>false</msPeap:RequireCryptoBinding>
                                    <msPeap:PeapExtensions />
                                </msPeap:EapType>
                            </baseEap:Eap>
                        </Config>
                    </EapHostConfig>
                </EAPConfig>
            </OneX>
        </security>
    </MSM>
</WLANProfile>

我是这样阅读的:

XDocument xdoc = XDocument.Load("xmlfile1.xml");
xdoc.Root.Element("name")

它返回 null 元素。

最佳答案

您必须考虑 XML 命名空间:

XNamespace ns = "http://www.microsoft.com/networking/WLAN/profile/v1";
XElement name = xdoc.Root.Element(ns + "name");

关于c# - 如何从以下文件中读取 xml 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28431450/

相关文章:

c# - 如何强制表单焦点?

c# - 在 C# 中处理跨线程事件的最佳方法是什么

c# - 在 C# 中使用 DLL

java - 我可以将字符串资源从 string.xml 访问到 Android 中的 Activity 中吗?

xml - 使用xslt转换嵌套的子节点

html - 是什么阻止我在 HTML 中使用任意标签?

Android - 不透明度淡入淡出可绘制

c# - 我应该压缩内存中的 C# 对象以获得更好的性能吗?

C# 如何创建一个调用另一个泛型方法的委托(delegate)的方法

java - 简单 XML : ValueRequiredException