【问题标题】:XmlDocument difficulties returning specific nodes/elements返回特定节点/元素的 XmlDocument 困难
【发布时间】:2013-11-26 16:00:28
【问题描述】:

第一次发帖。我希望它符合提问的规则。

我对 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);
                                }
                            }

                        }
                    }

提前感谢您的帮助

【问题讨论】:

    标签: xml-namespaces xmldocument


    【解决方案1】:

    所以我终于想通了。

    这是获取子节点的代码。基本上我没有使用我的名称空间标识符或我的名称空间来返回“源”节点中的子节点。

    对于处于这种情况的其他人,

    当你声明你的命名空间时,它有一部分,命名空间标识符是你想要的任何东西,在我的例子中,我选择了“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);
                                }
    

    我希望这可以帮助那些像我一样追尾的人。

    谢谢

    【讨论】:

      猜你喜欢
      • 2015-06-07
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 2021-05-18
      • 2014-11-25
      • 2016-02-07
      相关资源
      最近更新 更多