gpt4 book ai didi

c# - 正确阅读 XML 有困难

转载 作者:太空宇宙 更新时间:2023-11-03 14:08:29 26 4
gpt4 key购买 nike

我正在阅读网站上的 XML 文件。 channel 的名称及其“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>

效果很好。这样做的全部目的是检查 isLive 是否为真然后 OTVonlineStreams++;否则如果为假 OTVonlineStreams--;

但是,我需要使用 http://api.justin.tv/api/stream/list.xml?channel=我正在努力用上面使用的方法来做到这一点。在线时的xml文件是这样的:

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

离线时,XML 只是:

<results/>

我如何像使用 own3d.tv 一样使用 justin.tv? justin.tv 真正需要做的就是检查是否有除 <results/> 之外的 XML。 ,如果有的话,做 OTVonlineStreams++ 等。我不知道从哪里开始,我尝试了许多其他读取文件的方法,我可以,但后来我很难更新我所有的东西,比如 OTVonlineStreams++ 等。

我知道这里不是专门问这类问题的地方,也不是让别人替我做的地方,但我真的想不通。我正在学习 C#,所以请放轻松 :] 所以主要的问题是,我如何像阅读 own3d.tv 一样准确地阅读 justin.tv?

这里有一个指向 pastebin 的链接,其中包含我所说的整个文件的内容:http://pastebin.com/hZex2UBg

最佳答案

对于您自己的 3d.tv 查询,这是一个您可以使用 LINQ 单行代码而不是手动解析 XML 的好地方;

int count = (from e in doc.Descendants("isLive") where (bool)e select e).Count();

对于你的第二个请求,试试这个;

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

在此处查看有关 XDocument 的文档(即上面示例中的 doc); http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx

关于c# - 正确阅读 XML 有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8590707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com