[英]Reader Nodes from XML
I have this XML and i try to read the scpefic nodes but not work =( 我有这个XML,我尝试读取scpefic节点但不工作=(
the my code is: 我的代码是:
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);
the two circled values on image
图像上的两个圆圈值
Url: https://apps.db.ripe.net/whois/search.xml?query-string=193.200.150.125&source=ripe 网址: https : //apps.db.ripe.net/whois/search.xml? query-string = 193.200.150.125 & source =
it is possible? 有可能的?
When you are looking for a node which has an attribute "name" set to a particular value, you need to use different syntax. 当您查找具有设置为特定值的属性“name”的节点时,您需要使用不同的语法。
You're looking for something like: 你正在寻找类似的东西:
XmlNode node = doc.SelectSingleNode("/whois-resources/objects/object/attributes/attribute[@name=\"descr\"]");
XmlAttribute attrib = node.Attributes["value"];
MessageBox.Show(attrib.Value);
This will select your second node example, get the value of the value attribute, and display it. 这将选择您的第二个节点示例,获取value属性的值,并显示它。
How about using Linq To Xml? 如何使用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();
or 要么
var desc = xDoc.XPathSelectElements("//attribute[@name='descr']")
.Select(a => a.Attribute("value").Value)
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.