Chr*_*ris 7 c# xml getelementbyid
我有一个有效的XML文件,由以下.NET C#Windows服务读取.有问题的标签(u1_000)绝对在元素中:
<book id="u1_000" category="xyz">
Run Code Online (Sandbox Code Playgroud)
是否有某些原因GetElementById()找不到带有标记的Book元素?- 谢谢
XmlDocument doc = new XmlDocument();
doc.Load("C:\\j.xml");
XmlElement ee = doc.GetElementById("U1_000");
<book id="U1_000" category="web">
Run Code Online (Sandbox Code Playgroud)
如果不出意外,也许使用 xpath 作为备份:
string id = "u1_000";
string query = string.Format("//*[@id='{0}']", id); // or "//book[@id='{0}']"
XmlElement el = (XmlElement)doc.SelectSingleNode(query);
Run Code Online (Sandbox Code Playgroud)