我想解析一個XML文檔。有問題的文檔是一個AppxManifest文件。無法獲取XmlDocument.SelectNodes來檢索我的任何節點?
一個例子文件看起來是這樣的:
<?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>
我嘗試分析它像這樣:
var xml=new XmlDocument();
xml.Load(myfile);
var mgr=new XmlNamespaceManager(xml.NameTable);
mgr.AddNamespace("", "http://schemas.microsoft.com/appx/2010/manifest");
var nodes=xml.SelectNodes("Applications");
然而,當我執行此,nodes
永遠不會包含任何東西。儘管如此,XML文檔已加載。按照預期使用SelectNodes("//*")
返回每個節點。我這裏有什麼問題?
我也嘗試了許多變化對XPath查詢如
/Package/Applications/Application
Applications/Application
Applications/*
沒有出現,雖然獲取單個節點。理想情況下,我希望節點包含所有Application
節點
您的標題討論XDocument,但您的代碼使用XmlDocument。如果你真的*使用LINQ to XML,我只是建議使用Descendants ...是LINQ to XML的一個選項嗎? –
@JonSkeet可悲的是,我們不能使用Linq到XML,因爲我們的目標是.Net 2.0。但是我打算把XmlDocument。我總是得到兩個困惑 – Earlz