Reputation: 155
I am reading XML files that are on websites. The name of the channel and it's "id" is stored in a .txt file and is used when reading the XML page. This is the code that works for me no problem with 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.");
}
}
When reading it's XML, it's something like this:
<own3dReply>
<liveEvent>
<isLive>true</isLive>
<liveViewers>96</liveViewers>
<liveDuration>115046</liveDuration>
</liveEvent>
</own3dReply>
Which works perfectly. The whole purpose of this is to check if isLive is true then OTVonlineStreams++; otherwise if false OTVonlineStreams--;
However, I need to use http://api.justin.tv/api/stream/list.xml?channel= and I'm struggling to do it with the method I used above. The xml file when online is like this:
<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>
When it's offline, the XML is just:
<results/>
How would I use the justin.tv one like the one I did with the own3d.tv one? All the justin.tv one really needs to do is check if there is XML other than <results/>
, if there is, do OTVonlineStreams++ etc. I have no idea where to begin with it and I've tried many other ways of reading the file, which I can, but then I struggle to update all my stuff like OTVonlineStreams++ etc.
I know this isn't the place to ask this type of question specifically and for someone to do it for me, but I really can't figure this out. I'm learning C# so go easy on me please :] So the major question is, how do I read the justin.tv one precisely like I did with own3d.tv?
Here's a link to pastebin containing the entire file's contents for what I'm talking about: http://pastebin.com/hZex2UBg
Upvotes: 2
Views: 282
Reputation: 47
as said before, you should use the Linq oner-liner - as C.McAtakney said.
I just completed his sample with that to perform to your example...
my "sample.xml" taking your sample xml ... hope that will help u :)
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
namespace xmlRead
{
class Program
{
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create("sample.xml");
XDocument myDoc = XDocument.Load(reader);
//checking if theire a results element ?
int count = myDoc.Descendants("results").Count();
if (count == 0)
{
//do what your programs does to do
//for example reading embed_enabled and put in variable declared before :)
XElement embededEnabled = (from xml2 in myDoc.Descendants("embed_enabled") select xml2).FirstOrDefault();
Console.WriteLine("item to: {0}", embededEnabled);
}
Console.ReadLine();
}
}
}
Upvotes: 0
Reputation: 5195
I'm not sure if I got it right, but I think what could help you is an XSLT Transformation.
That means, you are using a transformation for each of your TV services, which is transforming the provider specific XML into a standardized XML format that can be used by your code.
This way you can easily add other providers later on and your code stays the same, regardless which provider you are using. Nice benefit is you are skipping all the data you don't need and you can do simple calculations/condition checks in the transformation.
This way you could check if the node <results/>
exists and then output <isLive>true</isLive>
or <isLive>false</isLive>
.
Have a look here: http://www.w3schools.com/xsl/
And an example for transforming XML to XML: http://en.wikipedia.org/wiki/XSLT#Example_1_.28transforming_XML_to_XML.29
Upvotes: 0
Reputation: 5222
For your own3d.tv query, this is a nice place where you can use a LINQ one-liner instead of manually parsing the XML;
int count = (from e in doc.Descendants("isLive") where (bool)e select e).Count();
For your second request, try this;
int count = doc.Descendants("stream").Count();
Take a look here for documentation on XDocument (which is what doc
is in the example above); http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx
Upvotes: 1