获取属性选择错误

时间:2018-02-05 14:19:41

标签: xml xpath

我有这个XML代码

<diffgr:diffgram xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1' FID='004'>
<Data>
   <users name='peter' UID='10003' diffgr:hasChanges='modified'/>
</Data>
</diffgr:diffgram>

我可以通过执行以下操作获取name属性:

SelectSingleNode("/*/Data/*/@name");

然后打印该名称的值。 但我想要属性diffgr:hasChanges

的值

我试着做类似的

SelectSingleNode("/*/Data/*/@diffgr:hasChanges");

但是在尝试打印该值时,我收到错误

    Unhandled Exception:
System.Xml.XPath.XPathException: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
  at MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree () <0x41689000 + 0x0003f> in <filename unknown>:0 
  at System.Xml.XPath.XPathNavigator.Evaluate (System.Xml.XPath.XPathExpression expr, System.Xml.XPath.XPathNodeIterator context) <0x41688e20 + 0x0007b> in <filename unknown>:0 
  at System.Xml.XPath.XPathNavigator.Evaluate (System.Xml.XPath.XPathExpression expr) <0x41688df0 + 0x0001a> in <filename unknown>:0 
  at System.Xml.XPath.XPathNavigator.Select (System.Xml.XPath.XPathExpression expr) <0x41688d70 + 0x0001b> in <filename unknown>:0 
  at System.Xml.XPath.XPathNavigator.Select (System.String xpath) <0x41684000 + 0x00026> in <filename unknown>:0 
  at System.Xml.XmlNode.SelectNodes (System.String xpath) <0x416839a0 + 0x00038> in <filename unknown>:0 
  at System.Xml.XmlNode.SelectSingleNode (System.String xpath) <0x41683950 + 0x00017> in <filename unknown>:0 
  at isoTopix.MyTest.Main (System.String[] args) [0x00018] in /home/tito/isoTopixSecurity/isoCode/console/tito.cs:28 
[ERROR] FATAL UNHANDLED EXCEPTION: System.Xml.XPath.XPathException: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
  at MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree () <0x41689000 + 0x0003f> in <filename unknown>:0 
  at System.Xml.XPath.XPathNavigator.Evaluate (System.Xml.XPath.XPathExpression expr, System.Xml.XPath.XPathNodeIterator context) <0x41688e20 + 0x0007b> in <filename unknown>:0 
  at System.Xml.XPath.XPathNavigator.Evaluate (System.Xml.XPath.XPathExpression expr) <0x41688df0 + 0x0001a> in <filename unknown>:0 
  at System.Xml.XPath.XPathNavigator.Select (System.Xml.XPath.XPathExpression expr) <0x41688d70 + 0x0001b> in <filename unknown>:0 
  at System.Xml.XPath.XPathNavigator.Select (System.String xpath) <0x41684000 + 0x00026> in <filename unknown>:0 
  at System.Xml.XmlNode.SelectNodes (System.String xpath) <0x416839a0 + 0x00038> in <filename unknown>:0 
  at System.Xml.XmlNode.SelectSingleNode (System.String xpath) <0x41683950 + 0x00017> in <filename unknown>:0 

我想我需要一些逃避结肠的cus。但是我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

如错误消息中所述

  

需要命名空间管理器或XsltContext。

您需要为diffgr设置命名空间。否则,属性@diffgr:hasChanges的表达式将不匹配。

在XSLT中,您将命名空间添加到根元素

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1'>

在带有XmlReader的C#中,您可以添加如下命名空间:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(reader.NameTable);
nsmgr.AddNamespace("diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1");    
XElement node = root.XPathSelectElement("/*/Data/*/@diffgr:hasChanges", nsmgr);

如果这没有帮助,请尝试以下答案:Namespace Manager or XsltContext needed