Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml SelectSingleNode:跳过意外的xpath_Xml_Xml Parsing_Xsd_Selectsinglenode_Parsexml - Fatal编程技术网

Xml SelectSingleNode:跳过意外的xpath

Xml SelectSingleNode:跳过意外的xpath,xml,xml-parsing,xsd,selectsinglenode,parsexml,Xml,Xml Parsing,Xsd,Selectsinglenode,Parsexml,我正在尝试使用xpath从xml中读取值。 我已经创建了XmlNamespaceManager,并使用我的xml文件向其添加了所有前缀、uri对 我有一组XPath,我需要遍历所有XPath并从xml中搜索相关数据 我正在使用下面的代码 if (myXmlDocument.DocumentElement != null) { var selectSingleNode = myXmlDocument.DocumentElement.SelectSingleNode(xPath, myNam

我正在尝试使用xpath从xml中读取值。 我已经创建了XmlNamespaceManager,并使用我的xml文件向其添加了所有前缀、uri对

我有一组XPath,我需要遍历所有XPath并从xml中搜索相关数据

我正在使用下面的代码

if (myXmlDocument.DocumentElement != null)
{
    var selectSingleNode = myXmlDocument.DocumentElement.SelectSingleNode(xPath, myNamespaceManager);
    if (selectSingleNode != null) 
        value = selectSingleNode.InnerText; 
}
我的问题是–当我传递这样的xpath时,其前缀不包含在xml中(因此我的namespaceManager不包含其前缀或命名空间),它将抛出一个异常“命名空间前缀'XXX'未定义”

我只想跳过这种意想不到的体验


有什么好的解决办法吗

作为一个快速修复方法,我使用try-catch并检查异常是否属于XPathException类型,消息是否包含“Namespace-prefix*is-defined”,然后跳过它,但我真的不喜欢这样