如何解析此 XML 中的信息?

发布于 2024-09-28 22:46:02 字数 388 浏览 3 评论 0原文

这是我想要抓取的 XML 示例:

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

请注意,contactinformation 标记有许多 contact 元素,每个元素相似但具有不同的值。

例如,其中包含 AIM 内容的元素,如何获取与 AIM 内容元素属于同一族的 Value 标签的内容?

这就是我被困住的地方。谢谢!

基本上:我需要找到 AIM 内容标签,记下它的位置,并找到同一系列中的 Value 元素。希望这能让问题更清楚

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 element?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

So尛奶瓶 2024-10-05 22:46:02

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

这将为您提供一个字符串列表,其中保存标题为 AIM 的联系人的值元素的值,如果您认为应该只有 1,则可以执行 First() 或 FirstOrDefault

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

草莓味的萝莉 2024-10-05 22:46:02

使用如下所示的 xpath 将为您提供联系人/值节点,其中联系人/标题为“AIM”:

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

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
苏辞 2024-10-05 22:46:02

您是否尝试过解析 XML 而不是“抓取”它?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文