如何获取下一个 XML 元素值?

发布于 2025-01-02 09:59:13 字数 734 浏览 0 评论 0原文

XDocument coordinates = XDocument.Load("http://feeds.feedburner.com/TechCrunch");
System.IO.StreamWriter StreamWriter1 = new System.IO.StreamWriter(DestFilePath);

foreach (var coordinate in coordinates.Descendants("guid"))
                {
                    string Links = coordinate.Value;                 
                    StreamWriter1.WriteLine(Links + Environment.NewLine );
                }

StreamWriter1.Close();

使用上述 URL (http://feeds.feedburner.com/TechCrunch) 的代码我可以获得所有链接,但我还想获得<描述>和<内容:编码>元素值。

问题是我想获得<描述>等值及其 guid 值,以便我可以将它们连续存储(在数据库中)。

我应该为此目的使用LINQ吗? 但请如何告诉?

XDocument coordinates = XDocument.Load("http://feeds.feedburner.com/TechCrunch");
System.IO.StreamWriter StreamWriter1 = new System.IO.StreamWriter(DestFilePath);

foreach (var coordinate in coordinates.Descendants("guid"))
                {
                    string Links = coordinate.Value;                 
                    StreamWriter1.WriteLine(Links + Environment.NewLine );
                }

StreamWriter1.Close();

Using this code for the above URL (http://feeds.feedburner.com/TechCrunch) i am able to obtain all the links but i also want to obtain <description> and <content:encoded> element values.

The problem is that i want to obtain <description> etc values along with their guid values so that i can store them in serially (in database).

Should i use LINQ of something for this purpose ?
But how please tell ?

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

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

发布评论

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

评论(4

寻梦旅人 2025-01-09 09:59:13

您应该迭代每个“项目”并检索其属性。不要忘记“内容”命名空间。

  XNamespace nsContent = "http://purl.org/rss/1.0/modules/content/";
  XDocument coordinates = XDocument.Load("http://feeds.feedburner.com/TechCrunch");

  foreach (var item in coordinates.Descendants("item"))
  {
          string link = item.Element("guid").Value;
          string description = item.Element("description").Value;
          string content = item.Element(nsContent + "encoded").Value;

  }

You should iterate over each "item" and retrieve its properties. Do not forget about the "content" namespace.

  XNamespace nsContent = "http://purl.org/rss/1.0/modules/content/";
  XDocument coordinates = XDocument.Load("http://feeds.feedburner.com/TechCrunch");

  foreach (var item in coordinates.Descendants("item"))
  {
          string link = item.Element("guid").Value;
          string description = item.Element("description").Value;
          string content = item.Element(nsContent + "encoded").Value;

  }
浮生未歇 2025-01-09 09:59:13

一种方法是您可以尝试单独枚举它们,

foreach (var coordinate in coordinates.Descendants())
    {
    foreach (var element in coordinate.Elements("description"))
    {
            string Links = element.Value;                 
            StreamWriter1.WriteLine(Links + Environment.NewLine );
    }
            foreach (var element in coordinate.Elements("guid"))
    {
            string Links = element.Value;                 
            StreamWriter1.WriteLine(Links + Environment.NewLine );
    }   
    //.................         
    }

One way is you can try enumerating them individualy,

foreach (var coordinate in coordinates.Descendants())
    {
    foreach (var element in coordinate.Elements("description"))
    {
            string Links = element.Value;                 
            StreamWriter1.WriteLine(Links + Environment.NewLine );
    }
            foreach (var element in coordinate.Elements("guid"))
    {
            string Links = element.Value;                 
            StreamWriter1.WriteLine(Links + Environment.NewLine );
    }   
    //.................         
    }
作业与我同在 2025-01-09 09:59:13

不确定,但尝试 .DescendantsAndSelf()

Not sure but try .DescendantsAndSelf()

美煞众生 2025-01-09 09:59:13

我建议您使用 XPATH 迭代每个 //content/item,然后获取该项目的 guid、内容等。

using System;
using System.Net;
using System.Xml;

namespace TechCrunch
{
  class Program
  {
    public static void Main(string[] args)
    {
      Console.WriteLine("Hello World!");

      try
      {
        HttpWebRequest request = HttpWebRequest.CreateHttp(
          "http://feeds.feedburner.com/TechCrunch");
        WebResponse response = request.GetResponse();

        XmlDocument feedXml = new XmlDocument();
        feedXml.Load(response.GetResponseStream());

        XmlNodeList itemList = feedXml.SelectNodes("//channel/item");
        Console.WriteLine("Found " + itemList.Count + " items.");

        foreach(XmlNode item in itemList)
        {
          foreach(XmlNode child in item.ChildNodes)
          {
            Console.WriteLine("There is a child named " + child.Name);
          }
        }
      }
      catch(Exception ex)
      {
        Console.WriteLine(ex.ToString());
      }

      Console.Write("Press any key to continue . . . ");
      Console.ReadKey(true);
    }
  }
}

I suggest that you use XPATh to iterate over each //content/item and then get that item's guid, content, etc.

using System;
using System.Net;
using System.Xml;

namespace TechCrunch
{
  class Program
  {
    public static void Main(string[] args)
    {
      Console.WriteLine("Hello World!");

      try
      {
        HttpWebRequest request = HttpWebRequest.CreateHttp(
          "http://feeds.feedburner.com/TechCrunch");
        WebResponse response = request.GetResponse();

        XmlDocument feedXml = new XmlDocument();
        feedXml.Load(response.GetResponseStream());

        XmlNodeList itemList = feedXml.SelectNodes("//channel/item");
        Console.WriteLine("Found " + itemList.Count + " items.");

        foreach(XmlNode item in itemList)
        {
          foreach(XmlNode child in item.ChildNodes)
          {
            Console.WriteLine("There is a child named " + child.Name);
          }
        }
      }
      catch(Exception ex)
      {
        Console.WriteLine(ex.ToString());
      }

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