Xml SelectSingleNode:跳过意外的xpath
我正在尝试使用xpath从xml中读取值。 我已经创建了XmlNamespaceManager,并使用我的xml文件向其添加了所有前缀、uri对 我有一组XPath,我需要遍历所有XPath并从xml中搜索相关数据 我正在使用下面的代码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
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”,然后跳过它,但我真的不喜欢这样