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

时间:2015-02-10 12:15:38

标签: 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元素。

1 个答案:

答案 0 :(得分:4)

您必须考虑XML名称空间:

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