开发者

How can I parse the information from this XML?

开发者 https://www.devze.com 2023-01-22 02:34 出处:网络
this is an example of the XML I want to scrape: http://www.dreamincode.net/forums/xml.php?showuser=335389

this is an example of the XML I want to scrape:

http://www.dreamincode.net/forums/xml.php?showuser=335389

Notice that the contactinformation tag has many contact elements, each similar but with different values.

For example, the element that has the AIM content in it, how can I get the content of the Value tag that's in the same family as the AIM content ele开发者_StackOverflow中文版ment?

That's where I'm stuck. Thanks!

Basically: I need to find the AIM content tag, make a note of where it is, and find the Value element within that same family. Hope this makes the question clearer


LINQToXML

var doc = XDocument.Load(@"http://www.dreamincode.net/forums/xml.php?showuser=335389");
var aimElements = doc.Descendants("contact").Where(a=>a.Element("title").Value == "AIM").Select(a=>a.Element("value").Value);

this will give you a list of strings that hold the value of the value element for a contact that has the title AIM, you can do a First() or a FirstOrDefault if you believe there should only be 1


Using an xpath like the one below will get you the contact/value node where contact/title is "AIM":

/ipb/profile/contactinformation/contact[title='AIM']/value


Have you tried to parse the XML rather than "scraping" it?

0

精彩评论

暂无评论...
验证码 换一张
取 消