2013-11-26 24 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); 
          } 

我希望这可以帮助别人是在追自己的尾巴,因为我有。

谢谢