2014-10-02 69 views
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); 
    } 
} 

回答

2

你应该已经使用Load()方法,而不是LoadXml()从文件加载XML:

doc.Load(@"C:\Users\byilmaz\Desktop\SPSS_SITE\g.xml"); 
+0

愚蠢的我!我从未注意到 – emre 2014-10-02 11:59:44

相关问题