C# 正确读取XML有困难

C# 正确读取XML有困难,c#,xml,C#,Xml,我正在阅读网站上的XML文件。频道的名称及其“id”存储在.txt文件中,并在读取XML页面时使用。这是适用于我的代码own3d.tv没有问题: public void checkOnline() { try { XmlTextReader reader = new XmlTextReader("http://api.own3d.tv/liveCheck.php?live_id=" + OTVid); bool done = false;

我正在阅读网站上的XML文件。频道的名称及其“id”存储在.txt文件中,并在读取XML页面时使用。这是适用于我的代码own3d.tv没有问题:

public void checkOnline()
{
    try
    {
        XmlTextReader reader = new XmlTextReader("http://api.own3d.tv/liveCheck.php?live_id=" + OTVid);
        bool done = false;
        while (reader.Read() && !done)
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Text:
                    if (OTVstreamOnline != bool.Parse(reader.Value) && !OTVoverridden)
                    {
                        OTVstreamOnline = bool.Parse(reader.Value);
                        if (OTVstreamOnline)
                        {
                            OTVonlineStreams++;
                        }
                        else
                        {
                            OTVonlineStreams--;
                        }
                        OTVAnnounce();
                    }
                    break;

                case XmlNodeType.EndElement:
                    done = true;
                    Console.WriteLine(OTVnick + " online = " + OTVstreamOnline);
                    break;
            }
        }
    }
    catch (XmlException)
    {
        Console.WriteLine("File not found.");
    }
}
当读取XML时,它是这样的:

<own3dReply>
 <liveEvent>
  <isLive>true</isLive>
  <liveViewers>96</liveViewers>
  <liveDuration>115046</liveDuration>
 </liveEvent>
</own3dReply>
<streams>
 <stream>
  <broadcast_part>1</broadcast_part>
  <featured>True</featured>
  <channel_subscription>False</channel_subscription>
  <audio_codec>mp3</audio_codec>
  <embed_count>253</embed_count>
  <id href="/stream/show/2281837264.xml">2281837264</id>
  <category>gaming</category>
  <title>EG's DeMoN - Streaming DotA 2</title>
  <video_height>720</video_height>
  <site_count>116</site_count>
  <embed_enabled>True</embed_enabled>
  <up_time>Wed Dec 21 03:43:00 2011</up_time>
  <meta_game>Dota 2</meta_game>
  <format>live</format>
  <stream_type>live</stream_type>
  <channel_count>648</channel_count>
  <abuse_reported>False</abuse_reported>
  <video_width>1280</video_width>
  <geo>MY</geo>
  <name href="/stream/show/live_user_dotademon.xml">live_user_dotademon</name>
  <language>en</language>
  <stream_count>369</stream_count>
  <video_bitrate>1720.34375</video_bitrate>
 </stream
</streams>

真的
96
115046
这很好用。这样做的全部目的是检查isLive是否为true,然后检查OTVonlineStreams++;否则,如果为false,则为OTVonlineStreams--

然而,我需要使用,并且我正在努力使用上面使用的方法。联机时的xml文件如下所示:

<own3dReply>
 <liveEvent>
  <isLive>true</isLive>
  <liveViewers>96</liveViewers>
  <liveDuration>115046</liveDuration>
 </liveEvent>
</own3dReply>
<streams>
 <stream>
  <broadcast_part>1</broadcast_part>
  <featured>True</featured>
  <channel_subscription>False</channel_subscription>
  <audio_codec>mp3</audio_codec>
  <embed_count>253</embed_count>
  <id href="/stream/show/2281837264.xml">2281837264</id>
  <category>gaming</category>
  <title>EG's DeMoN - Streaming DotA 2</title>
  <video_height>720</video_height>
  <site_count>116</site_count>
  <embed_enabled>True</embed_enabled>
  <up_time>Wed Dec 21 03:43:00 2011</up_time>
  <meta_game>Dota 2</meta_game>
  <format>live</format>
  <stream_type>live</stream_type>
  <channel_count>648</channel_count>
  <abuse_reported>False</abuse_reported>
  <video_width>1280</video_width>
  <geo>MY</geo>
  <name href="/stream/show/live_user_dotademon.xml">live_user_dotademon</name>
  <language>en</language>
  <stream_count>369</stream_count>
  <video_bitrate>1720.34375</video_bitrate>
 </stream
</streams>

1.
真的
假的

对于您自己的3D.tv查询,这是一个很好的地方,您可以使用LINQ one liner而不是手动解析XML

int count = (from e in doc.Descendants("isLive") where (bool)e select e).Count();
对于你的第二个请求,试试这个

int count = doc.Descendants("stream").Count();

在这里查看XDocument的文档(上面的示例中就是
doc

我不确定是否正确,但我认为XSLT转换可以帮助您

这意味着,您正在为每个电视服务使用转换,即将特定于提供商的XML转换为代码可以使用的标准化XML格式

通过这种方式,您可以方便地在以后添加其他提供程序,并且无论使用哪个提供程序,代码都保持不变。很好的好处是您跳过了所有不需要的数据,并且可以在转换中执行简单的计算/条件检查。 这样,您可以检查节点
是否存在,然后输出
true
false

请看这里:

以及将XML转换为XML的示例:

如前所述,您应该使用Linq oner衬里-正如C.McTakney所说

我刚刚完成了他的样本,以执行您的示例

我的“sample.xml”正在获取您的示例xml。。。希望这能帮助你:)


嗨,谢谢你。我尝试了以下方法来测试它:我将如何在上面的示例中使用它?使用OTVonlineStrams++;等等,这就是我被卡住的地方,我如何像你说的那样使用上面的内容。很抱歉,回复太晚了。感谢到目前为止的帮助。Hi@user1104783:您的pastebin链接中的代码是否返回了您所期望的结果?根据pastebin中的代码,如果流在线,则返回1;如果流不在线,则返回0。这是我所需要的,但是我如何在这个问题中的例子中使用它呢?第一个。像这样的?这不起作用,但我的意思是:第一个pastebin中的代码如果找到isLive或stream,将返回1。例如,如果没有找到isLive或stream,则将返回0。我想在上面提到的pastebin示例中使用该方法。谢谢你的帮助。