C#操作使用xsd的xml文件时XPath表达式不正确的问题

原本操作没有用xsd的xml文件时,XPath可以找到节点,但给xml文件关联上.xsd文件(Schema规范),

就出错了,根据Xpath找不到节点了!

 

问题出现时的Plugin.xsd文件

<?xml version="1.0" encoding="utf-8"?> <xs:schema id="Plugin" targetNamespace="http://tempuri.org/Plugin" elementFormDefault="qualified" xmlns="http://tempuri.org/Plugin" xmlns:mstns="http://tempuri.org/Plugin" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Plugin"> <xs:complexType> <xs:sequence> <xs:element name="Name" type="xs:string" /> <xs:element name="Author" type="xs:string" /> <xs:element name="Version" type="xs:string" /> <xs:element name="Description" type="xs:string" /> </xs:sequence> <xs:complexType> <xs:element> </xs:schema>

 

 

问题出现时的xml文件

<?xml version="1.0" encoding="utf-8" ?> <Plugin xmlns="http://tempuri.org/Plugin" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tempuri.org/Plugin Plugin.xsd" > <Name>名称</Name> <Author>作者</Author> <Version>版本</Version> <Description>说明</Description> </Plugin>

 

 

问题已解决的Plugin.dtd文件

<?xml version="1.0" encoding="utf-8"?> <xs:schema id="Plugin" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Plugin"> <xs:complexType> <xs:sequence> <xs:element name="Name" type="xs:string" /> <xs:element name="Author" type="xs:string" /> <xs:element name="Version" type="xs:string" /> <xs:element name="Description" type="xs:string" /> </xs:sequence> <xs:complexType> <xs:element> </xs:schema>

 

问题已解决的xml文件

<?xml version="1.0" encoding="utf-8" ?> <Plugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Plugin.xsd" > // xsd和xml在同一目录 <Name>名称</Name> <Author>作者</Author> <Version>版本</Version> <Description>说明</Description> </Plugin>

若要为没有目标命名空间的 XML 架构指定位置,请使用 noNamespaceSchemaLocation 属性

因为如果使用命名空间,则再用SelectSingleNode(xpath)时会把节点元素的命名空间加上,

xpath匹配发生变化!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值