0
我嘗試了很多不同的方式,但它一直在說:根級數據無效。第1行,位置1. 我的問題是:如何正確添加名稱空間?如何正確地將名稱空間添加到xpath c#中?
如果我從xml中刪除命名空間,它的工作原理是完美的,但我不能這樣做,因爲xml文檔已被其他人創建。
這是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<outputTree xmlns="http://www.ibm.com/software/analytics/spss/xml/oms" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ibm.com/software/analytics/spss/xml/oms http://www.ibm.com/software/analytics/spss/xml/oms/spss-output-1.8.xsd">
<book>
<book id="bk101">
<author id="1">Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description> An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author id="2">Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies,an evil sorceress, and her own childhood to become
queen of the world.
</description>
</book>
</book>
</outputTree>
Ç
private void GetXMLData()
{
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"C:\Users\byilmaz\Desktop\SPSS_SITE\g.xml");
XmlNamespaceManager nsmanager = new XmlNamespaceManager(doc.NameTable);
nsmanager.AddNamespace("test", "http://www.ibm.com/software/analytics/spss/xml/oms");
XmlNodeList errorNodes = doc.SelectNodes("/test:outputTree/", nsmanager);
foreach (XmlNode errorNode in errorNodes)
{
//string errorCode = errorNode.Attributes["id"].Value;
//string errorMessage = errorNode.InnerText;
}
}
catch (Exception err)
{
throw (err);
}
}
愚蠢的我!我從未注意到 – emre 2014-10-02 11:59:44