微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 无法获取XmlDocument.SelectNodes以检索我的任何节点?

我试图解析 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);

原文地址:https://www.jb51.cc/csharp/93552.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐