C#读取xml文档 [英] C# reading through xml documents

查看:91
本文介绍了C#读取xml文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void checkOnline()
        {
            try
            {
                XmlTextReader reader = new XmlTextReader("http://api.own3d.tv/liveCheck.php?live_id=" + id);
                bool done = false;
                while (reader.Read() && !done)
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Text:
                            if (streamOnline != bool.Parse(reader.Value) && !overridden)
                            {
                                streamOnline = bool.Parse(reader.Value);
                                if (streamOnline)
                                {
                                    onlineStreams++;
                                }
                                else
                                {
                                    onlineStreams--;
                                }
                                Announce();
                            }
                            break;
                        case XmlNodeType.EndElement:
                            done = true;
                            Console.WriteLine(nick + " online = " + streamOnline); // prints streamnamehere online = true/false
                            break;

                    }
                }
            }
            catch (XmlException)
            {
                Console.WriteLine("File not found.");
            }
        }



例如,它读取的XML是:



the XML it reads from for example is:

<own3dReply>
 <liveEvent>
  <isLive>true</isLive>
  <liveViewers>91</liveViewers>
  <liveDuration>137990</liveDuration>
 </liveEvent>
</own3dReply>



它读取以查看isLive是否为真

但是,我需要从 http://api.justin.tv/api/stream/list读取XML .xml?channel = [^ ]

例如,它读取的XML是:



it reads to see if isLive is true

However, I need to read xml from http://api.justin.tv/api/stream/list.xml?channel=[^]

the XML that it reads from for example is:

<streams>
 <stream>
  <broadcast_part>1</broadcast_part>
  <featured>True</featured>
  <channel_subscription>True</channel_subscription>
  <audio_codec>mp3</audio_codec>
  <embed_count>73</embed_count>
  <id href="/stream/show/2283462752.xml">2283462752</id>
  <category>gaming</category>
   <title>Showcraft - Monday/Wednesday 19:00 GMT - Vote Totalbiscuit King of the Web for charity! - http://tinyurl.com/votetotalbiscuit</title>
   <video_height>720</video_height>
   <site_count>669</site_count>
   <embed_enabled>True</embed_enabled>
   <up_time>Wed Dec 21 11:00:28 2011</up_time>
   <meta_game>StarCraft II: Wings of Liberty</meta_game>
   <format>live</format>
   <stream_type>live</stream_type>
   <channel_count>1490</channel_count>
   <abuse_reported>False</abuse_reported>
   <video_width>1280</video_width>
   <geo>SE</geo>
   <name href="/stream/show/live_user_totalbiscuit.xml">live_user_totalbiscuit</name>
   <language>en</language>
   <stream_count>742</stream_count>
   <video_bitrate>1581.1953125</video_bitrate>
   <channel>
    <subcategory nil="true"/>
    <category>gaming</category>
    <id>12616386</id>
    <login>totalbiscuit</login>
    <title>SHOUTcraft</title>
    <status>Showcraft - Monday/Wednesday 19:00 GMT - Vote Totalbiscuit King of the Web for charity! - http://tinyurl.com/votetotalbiscuit</status>
    <tags/>
    <producer>true</producer>
    <category_title>Gaming</category_title>
    <subcategory_title nil="true"/>
    <producer>true</producer>
    <language>en</language>
    <timezone>Europe/London</timezone>
    <channel_url>http://www.justin.tv/totalbiscuit</channel_url>
    <mature nil="true"/>
    <image_url_huge>http://static-cdn.jtvnw.net/jtv_user_pictures/totalbiscuit-profile_image-e1a17a8ba4a361e3-600x600.png</image_url_huge>
    <image_url_large>http://static-cdn.jtvnw.net/jtv_user_pictures/totalbiscuit-profile_image-e1a17a8ba4a361e3-300x300.png</image_url_large>
    <image_url_medium>http://static-cdn.jtvnw.net/jtv_user_pictures/totalbiscuit-profile_image-e1a17a8ba4a361e3-150x150.png</image_url_medium>
    <image_url_small>http://static-cdn.jtvnw.net/jtv_user_pictures/totalbiscuit-profile_image-e1a17a8ba4a361e3-70x70.png</image_url_small>
    <image_url_tiny>http://static-cdn.jtvnw.net/jtv_user_pictures/totalbiscuit-profile_image-e1a17a8ba4a361e3-50x50.png</image_url_tiny>
    <screen_cap_url_huge>http://static-cdn.jtvnw.net/previews/live_user_totalbiscuit-630x473.jpg</screen_cap_url_huge>
    <screen_cap_url_large>http://static-cdn.jtvnw.net/previews/live_user_totalbiscuit-320x240.jpg</screen_cap_url_large>
    <screen_cap_url_medium>http://static-cdn.jtvnw.net/previews/live_user_totalbiscuit-150x113.jpg</screen_cap_url_medium>
    <screen_cap_url_small>http://static-cdn.jtvnw.net/previews/live_user_totalbiscuit-70x53.jpg</screen_cap_url_small>
    <embed_enabled>true</embed_enabled>
    <embed_code>    <object type="application/x-shockwave-flash" height="295" width="353" id="live_embed_player_flash" data="http://www.justin.tv/widgets/live_embed_player.swf?channel=totalbiscuit" bgcolor="#000000"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://www.justin.tv/widgets/live_embed_player.swf" /><param name="flashvars" value="start_volume=25&channel=totalbiscuit&auto_play=false" /></object>
    </embed_code>
    <views_count><& get mongo users.totalbiscuit.views_count 0 &></views_count>
    </channel>
    <broadcaster>fme</broadcaster>
    <video_codec>AVC</video_codec>
    <channel_view_count>10627022</channel_view_count>
 </stream>
</streams>



当流为 LIVE 时,它显示此XML.当它不存在时,XML文件中存在的所有内容是:



It ONLY shows this XML when the stream is LIVE. When it''s not live, all that exists in the XML file is:

<results/>



我的问题是,如何使用以上读取XML的方法读取此XML文件?我需要做的就是读取文件,如果找到



MY QUESTION IS, how can I use my above method of reading XML to read this XML file? All I need to do is read the file, if it finds

<results/>

,则根据我的代码,它必须处于脱机状态.如果找不到(或者找到

, then according to my code, it has to be offline. If I don''t find it (or rather find

<streams>

),则表示它在线.但是,我该如何使用与我的代码相同的方式来做呢?任何帮助将不胜感激.

), it would mean it''s online. But how do I do it the same way I did with my code? Any help would be appreciated.

推荐答案

您要么尝试通过使用某些标志在while循环中进行检查(bool firstElement = true,bool offline = false- > if(firstElement && reader.Name =="results"){offline = true; break;} firstElement = false;-我不确定此处的属性Name是否正确),或者可以使用XmlDocument和XPath-但随后您必须先阅读整个流,然后才能对其进行处理.
当您拥有XmlDocument时,可以轻松检查是否存在特定节点.
Either you try to check this in the while-loop by using some flags (bool firstElement = true, bool offline = false --> if (firstElement && reader.Name == "results") { offline = true; break; } firstElement = false; - I am not sure whether the property Name is correct here) or you could use XmlDocument and XPath - but then you have to read the whole stream before you can process it.
When you have a XmlDocument you can easily check whether a specific node exists.


这篇关于C#读取xml文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆