2015-02-10 28 views
1

我有以下xml文件,我正在嘗試讀取name元素,但我無法讀懂該元素和其他元素嗎?如何從以下文件中讀取xml元素?

<?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元素。

回答

4

你必須採取XML命名空間考慮:

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