XmlTextReader - 如何迭代节点

发布于 2024-12-22 14:57:35 字数 1167 浏览 3 评论 0原文

我需要使用 XmlTextReader 循环浏览 XML 文档节点的帮助。不幸的是,除了 XmlTextReader 之外,使用其他任何东西都不是一个选择。

我的代码: <代码>

    class Program
    {
    private static void Main(string[] args)
    {
    XmlTextReader reader = new XmlTextReader("http://api.own3d.tv/liveCheck.php?live_id=180491");
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Text:
                        Console.WriteLine("Live: " + reader.Value);
                        break;
                }
            }
            Console.ReadLine();
        }
    }

<代码>

使用的 XML:

<own3dReply>
 <liveEvent>
  <isLive>true</isLive>
  <liveViewers>225</liveViewers>
  <liveDuration>1222</liveDuration>
 </liveEvent>
</own3dReply>

它输出到控制台的内容:

    Live: true
    Live: 225
    Live: 1222

它需要输出的内容:

    Live: true
    Viewers: 225
    Duration: 1222

它需要迭代每个节点并执行此操作,但我无法弄清楚。我尝试使用 switch 和 while 语句,但我似乎无法让它工作。

I need help with cycling through nodes of a XML document using XmlTextReader. Using anything else besides XmlTextReader is not an option, unfortunately.

My code:

    class Program
    {
    private static void Main(string[] args)
    {
    XmlTextReader reader = new XmlTextReader("http://api.own3d.tv/liveCheck.php?live_id=180491");
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Text:
                        Console.WriteLine("Live: " + reader.Value);
                        break;
                }
            }
            Console.ReadLine();
        }
    }

XML used:

<own3dReply>
 <liveEvent>
  <isLive>true</isLive>
  <liveViewers>225</liveViewers>
  <liveDuration>1222</liveDuration>
 </liveEvent>
</own3dReply>

What it's outputting to console:


    Live: true
    Live: 225
    Live: 1222

What it needs to output:


    Live: true
    Viewers: 225
    Duration: 1222

It needs to iterate through each node and do this, and I just can't figure it out. I tried using switch and while statements, but I just can't seem to get it to work.

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

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

发布评论

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

评论(1

鱼窥荷 2024-12-29 14:57:35

而不是:

Console.WriteLine("Live: " + reader.Value);

使用:

Console.WriteLine(string.Format("{0}: {1}", reader.LocalName, reader.Value));

LocalName 属性为您提供节点的本地名称(isLiveliveViewersliveDuration )。如果需要,您可以对这些进行更多字符串操作。

Instead of:

Console.WriteLine("Live: " + reader.Value);

Use:

Console.WriteLine(string.Format("{0}: {1}", reader.LocalName, reader.Value));

The LocalName property gives you the local name of the node (isLive, liveViewers and liveDuration). You can do more string manipulation on these if needed.

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