无法获取XmlDocument.SelectNodes找回我的任何节点? [英] Can't get XmlDocument.SelectNodes to retrieve any of my nodes?

查看:189
本文介绍了无法获取XmlDocument.SelectNodes找回我的任何节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析XML文档。有问题的文件是一个AppxManifest文件。

I'm trying to parse an XML document. The document in question is an AppxManifest file.

这是例子文件看起来是这样的:

An example document looks like this:

<?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>

我尝试分析它,像这样:

I try to parse it like so:

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(// *)返回预期的每一个节点。什么是我的问题吗?

However, after I execute this, nodes will never contain anything. The xml document is loaded and such though. using SelectNodes("//*") returns every node as expected. What is my problem here?

我也尝试了许多变化上的XPath查询,如

I've also tried many variations on that XPath query such as

  • /封装/应用程序/应用程序
  • 应用程序/应用程序
  • 应用程序/ *
  • /Package/Applications/Application
  • Applications/Application
  • Applications/*

没有出现,虽然获取单个节点。理想情况下,我想为节点,包含所有的应用节点

Nothing appears to retrieve the single node though. Ideally, I'd like for nodes to contain all of the Application nodes

推荐答案

您必须使用XML命名空间专门选择它们。考虑

You have to use xml namespace specifically to select them. consider

"//*[local-name()='Applications']/*[local-name()='Application']"    

在你的情况下,这code也可以很好地工作:

in your case this code may also work well:

        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);

这篇关于无法获取XmlDocument.SelectNodes找回我的任何节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆