【问题标题】:Reader Nodes from XML来自 XML 的阅读器节点
【发布时间】:2013-07-27 23:18:10
【问题描述】:

我有这个 XML,我尝试读取 scpefic 节点但不工作 =(

我的代码是:

XmlDocument doc = new XmlDocument();

            doc.Load("https://apps.db.ripe.net/whois/search.xml?query-string=193.200.150.125&source=ripe");

            XmlNode node = doc.SelectSingleNode("/whois-resources/objects/attributes/descr");

            MessageBox.Show(node.InnerText);

图片上带圆圈的两个值

网址:https://apps.db.ripe.net/whois/search.xml?query-string=193.200.150.125&source=ripe

有可能吗?

【问题讨论】:

    标签: c# xml xmlnode


    【解决方案1】:

    当您正在寻找一个将属性“name”设置为特定值的节点时,您需要使用不同的语法。

    你正在寻找类似的东西:

    XmlNode node = doc.SelectSingleNode("/whois-resources/objects/object/attributes/attribute[@name=\"descr\"]");
    
    XmlAttribute attrib = node.Attributes["value"];
    
    MessageBox.Show(attrib.Value);
    

    这将选择您的第二个节点示例,获取 value 属性的值,并显示它。

    【讨论】:

    • 感谢您的回答,但我明白消息框是空的
    【解决方案2】:

    使用 Linq To Xml 怎么样?

    var xDoc = XDocument.Load("https://apps.db.ripe.net/whois/search.xml?query-string=193.200.150.125&source=ripe");
    var desc = xDoc.Descendants("attribute")
                .Where(a => (string)a.Attribute("name") == "descr")
                .Select(a => a.Attribute("value").Value)
                .ToList();
    

    或

    var desc = xDoc.XPathSelectElements("//attribute[@name='descr']")
                .Select(a => a.Attribute("value").Value)
                .ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多