C# Xpath 错误具有无效令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10437684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xpath error has an Invalid Token
提问by user1373355
I have the following C# code:
我有以下 C# 代码:
var selectNode = xmlDoc.SelectSingleNode("//CodeType[@name='" + codetype +
"']/Section[@title='" + section + "']/Code[@code='" + code + "' and
@description='" + codedesc + "']") as XmlElement;
when I run my code it raises the error saying "the above statement has an invalid token"
当我运行我的代码时,它会引发错误,提示“上述语句具有无效令牌”
These are the values for the above statement.
这些是上述语句的值。
codeType=cbc
section="Mental"
codedesc="Injection, enzyme (eg, collagenase), palmar fascial cord (ie,
Dupuytren's contracture"
采纳答案by Cristian Lupascu
Notice the apostrophe (') in codedesc?
注意撇号(')的codedesc?
You need to escape it somehow. The XPath interpreter considers it as a string delimiter and doesn't know how to treat the other apostrophe after it.
你需要以某种方式逃避它。XPath 解释器将其视为字符串定界符,并且不知道如何处理其后的另一个撇号。
One way you can do that is by enclosing your string in double quotes instead of apostrophes.
一种方法是用双引号而不是撇号将字符串括起来。
Your code could therefore become:
因此,您的代码可能会变成:
var selectNode = xmlDoc.SelectSingleNode(
"//CodeType[@name='" + codetype + "']" +
"/Section[@title='" + section + "']" +
"/Code[@code=\"" + code + "' and @description='" + codedesc + "\"]")
as XmlElement;
(note that on the fourth line, the apostrophes(') became double quotes(\"))
(注意在第四行,撇号(')变成了双引号(\"))
While this approach works for the data you presented, you are still not 100% safe: other records could contain double quotes themselves. If that happens, we'll need to think of something for that case as well.
虽然这种方法适用于您提供的数据,但您仍然不是 100% 安全:其他记录本身可能包含双引号。如果发生这种情况,我们也需要为这种情况考虑一些事情。
回答by MGV
Duplicate the single quote, so that it reads "Dupuytren''s contracture"
复制单引号,使其显示为“Dupuytren 的挛缩”
This way you can escape the single quote in the xpath expression.
这样您就可以转义 xpath 表达式中的单引号。
回答by Jai Govind Gupta
You can get selected node based on index , if any special characters in the xml schema. So , here looks below implementation for delete selected index node from xml schema.
如果 xml 架构中有任何特殊字符,您可以根据 index 获取选定的节点。因此,下面是从 xml 模式中删除选定索引节点的实现。
XML SelectSingleNode Delete Operation
XML SelectSingleNode 删除操作
var schemaDocument = new XmlDocument();
var schemaDocument = new XmlDocument();
schemaDocument.LoadXml(codesXML);
var xmlNameSpaceManager = new XmlNamespaceManager(schemaDocument.NameTable);
if (schemaDocument.DocumentElement != null)
xmlNameSpaceManager.AddNamespace("x", schemaDocument.DocumentElement.NamespaceURI);
var codesNode = schemaDocument.SelectSingleNode(@"/x:integration-engine-codes/x:code-categories/x:code-category/x:codes", xmlNameSpaceManager);
var codeNode = codesNode.ChildNodes.Item(Convert.ToInt32(index) - 1);
if (codeNode == null || codeNode.ParentNode == null)
{
throw new Exception("Invalid node found");
}
codesNode.RemoveChild(codeNode);
return schemaDocument.OuterXml;

