.net烦恼小问题之XML读取异常

现有一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);

这样就没问题啦。

时间: 2024-08-27 03:44:56

.net烦恼小问题之XML读取异常的相关文章

Invalid byte 1 of 1-byte UTF-8 sequence(XML读取异常)

XML读取异常Invalid byte 1 of 1-byte UTF-8 sequence. 用xstream进行JavaBean与xml之间的转换,无法设定字符集编码格式,会导致上述异常. 解决方法:org.w3c.dom(java dom)解析xml文档,在此仅列出write方法: 1 public static void write(String xmlFile, String encoding) 2 throws ParserConfigurationException, FileNo

Xml读取异常--Invalid byte 1 of 1-byte UTF-8 sequence

xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence org.dom4j.DocumentException: Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence 出现这个问题是在我修改过xml文件后又重新保存后读取,文件出现这个问题,先看一下出现问题的代码 1 FileWriter fileWriter=new

简单的XML读取器

XML 指可扩展标记语言(EXtensible Markup Language) (有个很明显的槽点),是一种主要设计用来数据传输存储的语言. 有关语法规则我是参考了这个链接. http://www.w3school.com.cn/xml/xml_syntax.asp 看 gcc4 的时候觉得数据驱动编程很酷,于是顺带觉得xml很酷,正好暑假闲就写了个xml读取器看看,鼓捣了几天,弄好了大致上的功能,这里所说大致上,是能够满足检查语法错误并给出错误信息到标准输出设备(不保证报错信息绝对有用).如

SSH applicationContext.xml import异常

最近在项目上,遇到了一个问题,在配置applicationContext.xml使用<import>标签引入其它的xml文件时,导致项目启动时过慢,有时还会引起启动异常,后来查到是xml文件头中的设置问题,虽然不太知道标签头的作用,但还是和大家分享一下经验: applicationContext.xml文件,只贴出了部分代码: <!-- 文件头--> <beans xmlns="http://www.springframework.org/schema/beans&

C#写入对象到XML/从XML读取对象

1.将一个sentenceresult对象写入xml,并保存 private void writeToXml(sentenceresult sr) { System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(sentenceresult)); var path = Environment.GetFolderPath(Environment.SpecialFo

ACCESS(VBA)上的一个小项目 —— 2、读取ACCESS表数据到TreeView和ListView

有人问我能不能做一个程序的时候,我第一反应都说“能”. --这次在ACCESS中,借助TreeView和ListView做了一个数据联动的模型. 简析过程: 1)从网上找了一份TreeView学习教程<三小时学会树控件>,了解了TreeView的建立以及节点的使用方法: 2)把数据表中的某列按一定规则生成的数据再按一种规则解析成TreeView中的树结构(VPPS): 3)通过遍历TreeView中节点的折叠状态,通过上述一定规则生成的数据(唯一性)把数据表中的内容读取到ListView中:

微信公众号支付中出现XML数据异常的问题

注意:此处是xml数据异常,不是格式异常. 层层查找后发现WxpayAPI_php_v3/lib/WxPay.Api.php中notify方法中 $xml = $GLOBALS['HTTP_RAW_POST_DATA']; 此处$xml无法取得值,我改成 $xml = file_get_contents('php://input'); 成功解决问题.

C#基础笔记---浅谈XML读取以及简单的ORM实现

背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方案之一.下面简单谈谈XML的读取.  一. xml.linq读取xml 1.新建一个data.XML文件 1 <Customers> 2 <Customer> 3 <Name>Frank</Name> 4 <City>成都</City>

ssh框架中.xml文件小技巧分离xml

struts.xml文件 struts.xml文件里的action可以分离出来,如: <!-- 预警信息监测 --> <include file="config/struts/warningInformAtion-struts.xml"></include> 注: include是放在</struts>标签的前面 在src下面新建 package 名为:config.struts,再新建xml文件为warningInformAtion-s