我试图解析
XML文档.该文件是一个AppxManifest文件.
示例文档如下所示:
<?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");
但是,在执行此操作后,节点将不会包含任何内容. xml文档被加载,并且这样.使用SelectNodes(“// *”)按预期方式返回每个节点.这里有什么问题?
我也尝试过XPath查询上的许多变体
> / Package / Applications / Application
>应用/应用
>应用/ *
没有什么可以检索单个节点.理想情况下,我希望节点包含所有的应用程序节点
解决方法
您必须专门使用xml命名空间来选择它们.考虑
"//*[local-name()='Applications']/*[local-name()='Application']"
在你的情况下,这段代码也可能很好:
var doc = new XmlDocument(); doc.LoadXml(xml); var nsmgr = new XmlNamespaceManager(doc.NaMetable); nsmgr.AddNamespace("a","http://schemas.microsoft.com/appx/2010/manifest"); var nodes = doc.SelectNodes("//a:Applications/a:Application",nsmgr);