Question

I have this XML and i try to read the scpefic nodes but not work =(

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);

enter image description herethe two circled values on image

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

it is possible?

Was it helpful?

Solution

When you are looking for a node which has an attribute "name" set to a particular value, you need to use different syntax.

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.

OTHER TIPS

How about using 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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top