.net煩惱小問題之XML讀取異常

來源:互聯網
上載者:User

標籤:

現有一xml:

<?xml version="1.0" encoding="utf-8"?><Package xmlns="http://schemas.microsoft.com/appx/2010/manifest" xmlns:build="http://schemas.microsoft.com/developer/appx/2012/build" IgnorableNamespaces="build">  <Identity Name="uytury" Publisher="hygj" Version="1.0.0.12" ProcessorArchitecture="neutral" />  <Properties>    <DisplayName>jhjj</DisplayName>    <PublisherDisplayName>bhhjb</PublisherDisplayName>    <Logo>Assets\StoreLogo.png</Logo>  </Properties>  <Prerequisites>    <OSMinVersion>6.2.1</OSMinVersion>    <OSMaxVersionTested>6.2.1</OSMaxVersionTested>  </Prerequisites>  <Resources>    <Resource Language="EN" />  </Resources>  <Applications>    <Application Id="App" Executable="gfg.exe" EntryPoint="gfg.App">      <VisualElements DisplayName="fdsf" Logo="Assets\Logo.png" SmallLogo="Assets\SmallLogo.png" Description="gfdsg" ForegroundText="light" BackgroundColor="#2672EC">        <DefaultTile ShowName="allLogos" WideLogo="Assets\WideLogo.png" ShortName="gfdsg" />        <SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="#2672EC" />        <InitialRotationPreference>          <Rotation Preference="portrait" />          <Rotation Preference="landscape" />          <Rotation Preference="portraitFlipped" />          <Rotation Preference="landscapeFlipped" />        </InitialRotationPreference>      </VisualElements>      <Extensions>        <Extension Category="windows.search" />        <Extension Category="windows.shareTarget">          <ShareTarget>            <DataFormat>Text</DataFormat>          </ShareTarget>        </Extension>      </Extensions>    </Application>  </Applications>  <build:Metadata>    <build:Item Name="TargetFrameworkMoniker" Value=".NETCore,Version=v4.5" />    <build:Item Name="VisualStudio" Version="11.0" />    <build:Item Name="OperatingSystem" Version="6.2.9200.16384 (win8_rtm.120725-1247)" />    <build:Item Name="Microsoft.Build.AppxPackage.dll" Version="11.0.50727.1" />    <build:Item Name="Microsoft.Windows.UI.Xaml.Build.Tasks.dll" Version="11.0.50727.1" />  </build:Metadata></Package>

 

現需要讀取Application節點,艘衣賊~~於是寫下如下代碼:

XmlDocument doc = new XmlDocument();                      doc.Load(@"file");XmlNodeList itemList = doc.DocumentElement.SelectNodes("//Package/Applications/Application");

 結果發現itemList是空的。怎麼回事呢?因為該xml中包含有"xmlns"。我們在尋找節點之前也得先設定下namespace:

            XmlDocument doc = new XmlDocument();            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);            nsmgr.AddNamespace("a", "http://schemas.microsoft.com/appx/2010/manifest");            doc.Load(@"file");            XmlNodeList itemList = doc.DocumentElement.SelectNodes("//a:Package/a:Applications/a:Application",nsmgr);

這樣就沒問題啦。

 

.net煩惱小問題之XML讀取異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.