我有以下 XML 檔案
<?xml version="1.0" ?>
<server xmlns="exampleServer">
<system-properties>
<property name="prop0" value="null"/>
<property name="prop1" value="true"/>
<property name="prop2" value="false"/>
</system-properties>
</server>
還有這個。代碼:
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement root = doc.DocumentElement;
XmlNodeList nodeList = root.SelectNodes("//system-properties/property");
但是 nodeList.Count 總是 0。我試過了
root.SelectNodes("/system-properties/property");
root.SelectNodes("system-properties/property");
root.SelectNodes("//property");
..aso 但沒有任何效果。對 var 的評估顯示 Childnodes 但沒有選擇。
我做錯了什么?
uj5u.com熱心網友回復:
問題是您的 XML: 中有一個命名空間<server xmlns="exampleServer">。使用 XPath 時,如果您不在路徑中添加前綴,則假定元素沒有命名空間。請參閱相關問題的答案。
在您的情況下,您需要在 XPath 中使用XmlNamespaceManager并指定要搜索的元素的命名空間:
var nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("es", "exampleServer");
var properties = root.SelectNodes("//es:system-properties/es:property", nsMgr);
請參閱this fiddle進行測驗運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459333.html
上一篇:排除XSLT的(1.0)行回傳和文本輸出中的額外空格
下一篇:如何使MaterialTextView的寬度wrap_content但不大于ConstraintLayout中的MaterialButton?
