Skip to main content
 首页 » 编程设计

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

2024年02月06日17lori

我有以下 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");