2013-11-26 26 views
0

第一篇文章。我希望它符合提問的規則。XmlDocument返回特定節點/元素的困難

我有點困擾xml文檔(它的API返回了Xml)。現在它使用了大量基於互聯網(http的)安全措施,我已經努力了,現在我能夠返回頂層的未嵌套的節點。

但是有幾個節點嵌套在這些下面,我需要返回一些這些值。

我設置使用XMLDocument來做到這一點,我對使用XPath不感興趣。

我還應該注意到我使用的是.Net 4.5環境。

示例XML

<?xml version="1.0" encoding="utf-8"?> 
<results> 
    <Info xmlns="http://xmlns.namespace"> 
     <title>This Title</title> 
    <ref> 
     <SetId>317</SetId> 
    </ref> 
    <source> 
     <name>file.xxx</name> 
     <type>thisType</type> 
     <hash>cc7b99599c1bebfc4b8f12e47aba3f76</hash> 
     <pers>65.97602</pers> 
     <time>02:20:02.8527777</time> 
    </source> 
    ....... Continuation which is same as above 

好了,所以上面是獲取從API返回的XML,現在,我可以回到標題節點沒有問題。我還想返回的是元素中的任何節點值,例如pers節點值。但我只想返回一個(因爲現有的xml中有很多下降)

請注意,Info節點中有一個xmlns,它可能不允許我返回值。

因此,這裏是我的代碼提前

using (var response = (HttpWebResponse) request.GetResponse()) 
     { 
      //Get the response stream 
      using (Stream stream = response.GetResponseStream()) 
      { 
       if (stream != null) 
       { 
        var xDoc = new XmlDocument(); 

        var nsm = new XmlNamespaceManager(xDoc.NameTable); 
        nsm.AddNamespace("ns", XmlNamespace); 

        //Read the response stream 
        using (XmlReader xmlReader = XmlReader.Create(stream)) 
        { 
         // This is straight forward, we just need to read the XML document and return the bits we need. 
         xDoc.Load(xmlReader); 
         XmlElement root = xDoc.DocumentElement; 
         var cNodes = root.SelectNodes("/results/ns:Info", nsm); 

         //Create a new instance of Info so that we can store any data found in the Info Properties. 
         var info = new Info(); 

         // Now we have a collection of Info objects 
         foreach (XmlNode node in cNodes) 
         { 
          // Do some parsing or other relevant filtering here 
          var title = node["title"]; 
          if (title != null) 
          { 
           info.Title = title.InnerText; 
           _logger.Info("This is the title returned ############# {0}", info.Title); 
          } 

          //This is the bit that is killing me as i can't return the any values in the of the sub nodes 
          XmlNodeList sourceNodes = node.SelectNodes("source"); 
          foreach (XmlNode sn in sourceNodes) 
          { 
           XmlNode source = sn.SelectSingleNode("source"); 
           { 
            var pers = root["pers"]; 
            if (pers != null) info.pers = pers.InnerText; 
            _logger.Info("############FPS = {0}", info.pers); 
           } 
          } 

         } 
        } 

感謝所有幫助

回答

0

所以,我終於想通了。

下面是獲取子節點的代碼。基本上我沒有使用我的命名空間標識符或我的命名空間來返回「源」節點內的子節點。

對於在這種情況下任何人,

當你宣佈你的名字空間有零件給它,一個命名空間標識符是什麼,你希望它是在我的情況下,我選擇了「NS」,然後XML文件中的實際名稱空間(以xmlns爲前綴)將包含類似如下的內容:「http://xmlns.mynamespace」。

因此,當搜索頂層內的子節點時,您需要爲要獲取的子節點的主節點聲明這些命名空間。

// get the <source> subnode using the namespace to returns all <source> values 
          var source = node.SelectSingleNode("ns:source", nsm); 
          if (source != null) 
          { 
           info.SourceType = source["type"].InnerText; 
           info.Pers = source["pers"].InnerText; 

           _logger.Info("This SourceNode is {0}", info.SourceType); 
           _logger.Info("This PersNode is {0}", info.FramesPerSecond); 
          } 

我希望這可以幫助別人是在追自己的尾巴,因爲我有。

謝謝