1
我想從xml中使用xpath讀取值。 我已經創建了XmlNamespaceManager,並使用我的xml文件將所有前綴uri對添加到它。SelectSingleNode:跳過意想不到的xpath
我有一套xpaths與我,我需要遍歷所有xpaths和從xml搜索相關數據。
我使用下面的代碼
if (myXmlDocument.DocumentElement != null)
{
var selectSingleNode = myXmlDocument.DocumentElement.SelectSingleNode(xPath, myNamespaceManager);
if (selectSingleNode != null)
value = selectSingleNode.InnerText;
}
我的問題是 - 當我通過這樣的XPath,其前綴不是蓋的XML(所以我namespaceManager不包含其前綴或命名空間)內,它會拋出一個異常「名稱空間前綴'XXX'未定義。」
我只想跳過這種意外的xpath。
有沒有什麼好的解決方案呢?