From: <sam...@us...> - 2006-06-10 15:42:04
|
Revision: 12 Author: samuel337 Date: 2006-06-10 08:41:52 -0700 (Sat, 10 Jun 2006) ViewCVS: http://svn.sourceforge.net/mp-webinterface/?rev=12&view=rev Log Message: ----------- updated code on all XML-returning functions to return UTF8 encoded data (now more efficient too). Modified Paths: -------------- trunk/source/ECP2WebService/App_Code/Service.cs Modified: trunk/source/ECP2WebService/App_Code/Service.cs =================================================================== --- trunk/source/ECP2WebService/App_Code/Service.cs 2006-06-01 07:03:28 UTC (rev 11) +++ trunk/source/ECP2WebService/App_Code/Service.cs 2006-06-10 15:41:52 UTC (rev 12) @@ -33,7 +33,6 @@ return GetEMPH().SendActionByID(actionID); } - [WebMethod(Description="This command gets the current path to MediaPortal.")] public string GetMPPath() { @@ -263,6 +262,17 @@ return GetEMPH().ImportXMLTVGuide(); } + private System.Xml.XmlWriterSettings GetXMLDocWriterSettings() + { + System.Xml.XmlWriterSettings xwSettings = new System.Xml.XmlWriterSettings(); + xwSettings.Encoding = System.Text.Encoding.UTF8; + xwSettings.Indent = true; + xwSettings.OmitXmlDeclaration = false; + xwSettings.ConformanceLevel = System.Xml.ConformanceLevel.Auto; + + return xwSettings; + } + [WebMethod(Description="This command retrieves all the channels in MP in XML format.")] public string GetChannels() { @@ -279,31 +289,36 @@ xmlAttributes.XmlIgnore = true; xmlOverrides.Add(typeof(MediaPortal.TV.Database.TVChannel), "CurrentProgram", xmlAttributes); - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - System.IO.StringWriter sw = new System.IO.StringWriter(sb); - System.Text.StringBuilder sbXML = new System.Text.StringBuilder(); - System.IO.StringWriter swXML = new System.IO.StringWriter(sbXML); - System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(swXML); + System.IO.MemoryStream xwMem = new System.IO.MemoryStream(); + System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(xwMem, GetXMLDocWriterSettings()); System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MediaPortal.TV.Database.TVChannel),xmlOverrides); MediaPortal.TV.Database.TVChannel chn; - xw.WriteStartDocument(); - xw.WriteStartElement("MPData"); - xw.WriteStartElement("TVChannels"); + try + { + xw.WriteStartDocument(); + xw.WriteStartElement("MPData"); + xw.WriteStartElement("TVChannels"); - for (int i = 0; i <= chnList.Count - 1; i++) + for (int i = 0; i <= chnList.Count - 1; i++) + { + chn = (MediaPortal.TV.Database.TVChannel)chnList[i]; + + xs.Serialize(xw, chn); + } + + xw.WriteEndElement(); + xw.WriteEndElement(); + xw.WriteEndDocument(); + + xw.Flush(); + } + catch (Exception ex) { - chn = (MediaPortal.TV.Database.TVChannel)chnList[i]; - xs.Serialize(sw, chn); - xw.WriteRaw(sb.ToString().Substring(sb.ToString().IndexOf("?>") + 2)); - sb.Remove(0, sb.Length - 1); + return ex.ToString(); } - xw.WriteEndElement(); - xw.WriteEndElement(); - xw.WriteEndDocument(); - - return sbXML.ToString(); + return System.Text.Encoding.UTF8.GetString(xwMem.ToArray()); } [WebMethod(Description = "This command retrieves the specified channel from MP in XML format.")] @@ -320,27 +335,30 @@ xmlAttributes.XmlIgnore = true; xmlOverrides.Add(typeof(MediaPortal.TV.Database.TVChannel), "CurrentProgram", xmlAttributes); - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - System.IO.StringWriter sw = new System.IO.StringWriter(sb); - System.Text.StringBuilder sbXML = new System.Text.StringBuilder(); - System.IO.StringWriter swXML = new System.IO.StringWriter(sbXML); - System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(swXML); + System.IO.MemoryStream xwMem = new System.IO.MemoryStream(); + System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(xwMem, GetXMLDocWriterSettings()); System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MediaPortal.TV.Database.TVChannel), xmlOverrides); - xw.WriteStartDocument(); - xw.WriteStartElement("MPData"); - xw.WriteStartElement("TVChannels"); + try + { + xw.WriteStartDocument(); + xw.WriteStartElement("MPData"); + xw.WriteStartElement("TVChannels"); - xs.Serialize(sw, chn); + xs.Serialize(xw, chn); - xw.WriteRaw(sb.ToString().Substring(sb.ToString().IndexOf("?>") + 2)); - sb.Remove(0, sb.Length - 1); + xw.WriteEndElement(); + xw.WriteEndElement(); + xw.WriteEndDocument(); - xw.WriteEndElement(); - xw.WriteEndElement(); - xw.WriteEndDocument(); - - return sbXML.ToString(); + xw.Flush(); + } + catch (Exception ex) + { + return ex.ToString(); + } + + return System.Text.Encoding.UTF8.GetString(xwMem.ToArray()); } [WebMethod(Description="This command converts a channelID into its corresponding channel name.")] @@ -385,9 +403,8 @@ List<string> genresList; genresList = GetEMPH().GetGenres(); - System.Text.StringBuilder sbXML = new System.Text.StringBuilder(); - System.IO.StringWriter swXML = new System.IO.StringWriter(sbXML); - System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(swXML); + System.IO.MemoryStream xwMem = new System.IO.MemoryStream(); + System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(xwMem, GetXMLDocWriterSettings()); xw.WriteStartDocument(); xw.WriteStartElement("MPData"); @@ -406,8 +423,10 @@ xw.WriteEndElement(); xw.WriteEndElement(); xw.WriteEndDocument(); - - return sbXML.ToString(); + + xw.Flush(); + + return System.Text.Encoding.UTF8.GetString(xwMem.ToArray()); } [WebMethod(Description="This command returns the list of TV programs within the given date in XML format.")] @@ -416,35 +435,35 @@ List<MediaPortal.TV.Database.TVProgram> progList; progList = GetEMPH().GetPrograms(startDateTime,endDateTime); - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - System.IO.StringWriter sw = new System.IO.StringWriter(sb); - System.Text.StringBuilder sbXML = new System.Text.StringBuilder(); - System.IO.StringWriter swXML = new System.IO.StringWriter(sbXML); - System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(swXML); + System.IO.MemoryStream xwMem = new System.IO.MemoryStream(); + System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(xwMem, GetXMLDocWriterSettings()); System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MediaPortal.TV.Database.TVProgram )); MediaPortal.TV.Database.TVProgram prog; - xw.WriteStartDocument(); - xw.WriteStartElement("MPData"); - xw.WriteStartElement("TVPrograms"); - - for (int i = 0; i <= progList.Count - 1; i++) + try { - prog = (MediaPortal.TV.Database.TVProgram)progList[i]; - xs.Serialize(sw, prog); + xw.WriteStartDocument(); + xw.WriteStartElement("MPData"); + xw.WriteStartElement("TVPrograms"); - //write serialisation to xmltextwriter, without XML declaration line - xw.WriteRaw(sb.ToString().Substring(sb.ToString().IndexOf("?>") + 2)); + for (int i = 0; i <= progList.Count - 1; i++) + { + prog = (MediaPortal.TV.Database.TVProgram)progList[i]; + xs.Serialize(xw, prog); + } - //clear serialisation stringbuilder - sb.Remove(0, sb.Length - 1); + xw.WriteEndElement(); + xw.WriteEndElement(); + xw.WriteEndDocument(); + + xw.Flush(); } + catch (Exception ex) + { + return ex.ToString(); + } - xw.WriteEndElement(); - xw.WriteEndElement(); - xw.WriteEndDocument(); - - return sbXML.ToString(); + return System.Text.Encoding.UTF8.GetString(xwMem.ToArray()); } [WebMethod(Description="This command returns a list of all the TV programs in XML format.")] @@ -453,35 +472,35 @@ List<MediaPortal.TV.Database.TVProgram> progList; progList = GetEMPH().GetPrograms(); - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - System.IO.StringWriter sw = new System.IO.StringWriter(sb); - System.Text.StringBuilder sbXML = new System.Text.StringBuilder(); - System.IO.StringWriter swXML = new System.IO.StringWriter(sbXML); - System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(swXML); + System.IO.MemoryStream xwMem = new System.IO.MemoryStream(); + System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(xwMem, GetXMLDocWriterSettings()); System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MediaPortal.TV.Database.TVProgram )); MediaPortal.TV.Database.TVProgram prog; - xw.WriteStartDocument(); - xw.WriteStartElement("MPData"); - xw.WriteStartElement("TVPrograms"); - - for (int i = 0; i <= progList.Count - 1; i++) + try { - prog = (MediaPortal.TV.Database.TVProgram)progList[i]; - xs.Serialize(sw, prog); + xw.WriteStartDocument(); + xw.WriteStartElement("MPData"); + xw.WriteStartElement("TVPrograms"); - //write serialisation to xmltextwriter, without XML declaration line - xw.WriteRaw(sb.ToString().Substring(sb.ToString().IndexOf("?>") + 2)); + for (int i = 0; i <= progList.Count - 1; i++) + { + prog = (MediaPortal.TV.Database.TVProgram)progList[i]; + xs.Serialize(xw, prog); + } - //clear serialisation stringbuilder - sb.Remove(0, sb.Length - 1); + xw.WriteEndElement(); + xw.WriteEndElement(); + xw.WriteEndDocument(); + + xw.Flush(); } + catch (Exception ex) + { + return ex.ToString(); + } - xw.WriteEndElement(); - xw.WriteEndElement(); - xw.WriteEndDocument(); - - return sbXML.ToString(); + return System.Text.Encoding.UTF8.GetString(xwMem.ToArray()); } [WebMethod(Description="This command returns the list of TV program genres in XML format.")] @@ -490,35 +509,35 @@ List<MediaPortal.TV.Database.TVProgram> progList; progList = GetEMPH().GetPrograms(GetChannelName(channelID),startDateTime,endDateTime); - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - System.IO.StringWriter sw = new System.IO.StringWriter(sb); - System.Text.StringBuilder sbXML = new System.Text.StringBuilder(); - System.IO.StringWriter swXML = new System.IO.StringWriter(sbXML); - System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(swXML); + System.IO.MemoryStream xwMem = new System.IO.MemoryStream(); + System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(xwMem, GetXMLDocWriterSettings()); System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MediaPortal.TV.Database.TVProgram )); MediaPortal.TV.Database.TVProgram prog; - xw.WriteStartDocument(); - xw.WriteStartElement("MPData"); - xw.WriteStartElement("TVPrograms"); - - for (int i = 0; i <= progList.Count - 1; i++) + try { - prog = (MediaPortal.TV.Database.TVProgram)progList[i]; - xs.Serialize(sw, prog); + xw.WriteStartDocument(); + xw.WriteStartElement("MPData"); + xw.WriteStartElement("TVPrograms"); - //write serialisation to xmltextwriter, without XML declaration line - xw.WriteRaw(sb.ToString().Substring(sb.ToString().IndexOf("?>") + 2)); + for (int i = 0; i <= progList.Count - 1; i++) + { + prog = (MediaPortal.TV.Database.TVProgram)progList[i]; + xs.Serialize(xw, prog); + } - //clear serialisation stringbuilder - sb.Remove(0, sb.Length - 1); + xw.WriteEndElement(); + xw.WriteEndElement(); + xw.WriteEndDocument(); + + xw.Flush(); } + catch (Exception ex) + { + return ex.ToString(); + } - xw.WriteEndElement(); - xw.WriteEndElement(); - xw.WriteEndDocument(); - - return sbXML.ToString(); + return System.Text.Encoding.UTF8.GetString(xwMem.ToArray()); } [WebMethod(Description="This command returns the list of TV recordings in XML format.")] @@ -527,35 +546,35 @@ List<MediaPortal.TV.Database.TVRecording > recList; recList = GetEMPH().GetRecordings(); - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - System.IO.StringWriter sw = new System.IO.StringWriter(sb); - System.Text.StringBuilder sbXML = new System.Text.StringBuilder(); - System.IO.StringWriter swXML = new System.IO.StringWriter(sbXML); - System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(swXML); + System.IO.MemoryStream xwMem = new System.IO.MemoryStream(); + System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(xwMem, GetXMLDocWriterSettings()); System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MediaPortal.TV.Database.TVRecording )); MediaPortal.TV.Database.TVRecording rec; - xw.WriteStartDocument(); - xw.WriteStartElement("MPData"); - xw.WriteStartElement("TVRecordings"); - - for (int i = 0; i <= recList.Count - 1; i++) + try { - rec = (MediaPortal.TV.Database.TVRecording)recList[i]; - xs.Serialize(sw, rec); + xw.WriteStartDocument(); + xw.WriteStartElement("MPData"); + xw.WriteStartElement("TVRecordings"); - //write serialisation to xmltextwriter, without XML declaration line - xw.WriteRaw(sb.ToString().Substring(sb.ToString().IndexOf("?>") + 2)); + for (int i = 0; i <= recList.Count - 1; i++) + { + rec = (MediaPortal.TV.Database.TVRecording)recList[i]; + xs.Serialize(xw, rec); + } - //clear serialisation stringbuilder - sb.Remove(0, sb.Length - 1); + xw.WriteEndElement(); + xw.WriteEndElement(); + xw.WriteEndDocument(); + + xw.Flush(); } + catch (Exception ex) + { + return ex.ToString(); + } - xw.WriteEndElement(); - xw.WriteEndElement(); - xw.WriteEndDocument(); - - return sbXML.ToString(); + return System.Text.Encoding.UTF8.GetString(xwMem.ToArray()); } [WebMethod(Description="This command returns the list of all recorded TV items in XML format.")] @@ -564,35 +583,35 @@ List<MediaPortal.TV.Database.TVRecorded> recdTVList; recdTVList = GetEMPH().GetAllRecordedTV(); - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - System.IO.StringWriter sw = new System.IO.StringWriter(sb); - System.Text.StringBuilder sbXML = new System.Text.StringBuilder(); - System.IO.StringWriter swXML = new System.IO.StringWriter(sbXML); - System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(swXML); + System.IO.MemoryStream xwMem = new System.IO.MemoryStream(); + System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(xwMem, GetXMLDocWriterSettings()); System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MediaPortal.TV.Database.TVRecorded )); MediaPortal.TV.Database.TVRecorded recdTV; - xw.WriteStartDocument(); - xw.WriteStartElement("MPData"); - xw.WriteStartElement("TVRecorded"); - - for (int i = 0; i <= recdTVList.Count - 1; i++) + try { - recdTV = (MediaPortal.TV.Database.TVRecorded)recdTVList[i]; - xs.Serialize(sw, recdTV); + xw.WriteStartDocument(); + xw.WriteStartElement("MPData"); + xw.WriteStartElement("TVRecorded"); - //write serialisation to xmltextwriter, without XML declaration line - xw.WriteRaw(sb.ToString().Substring(sb.ToString().IndexOf("?>") + 2)); + for (int i = 0; i <= recdTVList.Count - 1; i++) + { + recdTV = (MediaPortal.TV.Database.TVRecorded)recdTVList[i]; + xs.Serialize(xw, recdTV); + } - //clear serialisation stringbuilder - sb.Remove(0, sb.Length - 1); + xw.WriteEndElement(); + xw.WriteEndElement(); + xw.WriteEndDocument(); + + xw.Flush(); } + catch (Exception ex) + { + return ex.ToString(); + } - xw.WriteEndElement(); - xw.WriteEndElement(); - xw.WriteEndDocument(); - - return sbXML.ToString(); + return System.Text.Encoding.UTF8.GetString(xwMem.ToArray()); } [WebMethod(Description = "This command returns the specified recorded TV item in XML format.")] @@ -601,30 +620,30 @@ MediaPortal.TV.Database.TVRecorded recdTV; recdTV = GetEMPH().GetRecordedTV(recordedTVFileName); - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - System.IO.StringWriter sw = new System.IO.StringWriter(sb); - System.Text.StringBuilder sbXML = new System.Text.StringBuilder(); - System.IO.StringWriter swXML = new System.IO.StringWriter(sbXML); - System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(swXML); + System.IO.MemoryStream xwMem = new System.IO.MemoryStream(); + System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(xwMem, GetXMLDocWriterSettings()); System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MediaPortal.TV.Database.TVRecorded)); - - xw.WriteStartDocument(); - xw.WriteStartElement("MPData"); - xw.WriteStartElement("TVRecorded"); - xs.Serialize(sw, recdTV); + try + { + xw.WriteStartDocument(); + xw.WriteStartElement("MPData"); + xw.WriteStartElement("TVRecorded"); - //write serialisation to xmltextwriter, without XML declaration line - xw.WriteRaw(sb.ToString().Substring(sb.ToString().IndexOf("?>") + 2)); + xs.Serialize(xw, recdTV); - //clear serialisation stringbuilder - sb.Remove(0, sb.Length - 1); + xw.WriteEndElement(); + xw.WriteEndElement(); + xw.WriteEndDocument(); - xw.WriteEndElement(); - xw.WriteEndElement(); - xw.WriteEndDocument(); + xw.Flush(); + } + catch (Exception ex) + { + return ex.ToString(); + } - return sbXML.ToString(); + return System.Text.Encoding.UTF8.GetString(xwMem.ToArray()); } [WebMethod(Description="This command returns the name of the current playlist.")] @@ -654,36 +673,36 @@ MediaPortal.Playlists.PlayList playlist; playlist = GetEMPH().GetPlaylist(playlistTypeResolve); - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - System.IO.StringWriter sw = new System.IO.StringWriter(sb); - System.Text.StringBuilder sbXML = new System.Text.StringBuilder(); - System.IO.StringWriter swXML = new System.IO.StringWriter(sbXML); - System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(swXML); + System.IO.MemoryStream xwMem = new System.IO.MemoryStream(); + System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(xwMem, GetXMLDocWriterSettings()); System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MediaPortal.Playlists.PlayListItem )); MediaPortal.Playlists.PlayListItem playlistItem; - xw.WriteStartDocument(); - xw.WriteStartElement("MPData"); - xw.WriteStartElement("Playlist"); - xw.WriteAttributeString("type", playlistType ); - - for (int i = 0; i <= playlist.Count - 1; i++) + try { - playlistItem = (MediaPortal.Playlists.PlayListItem )playlist[i]; - xs.Serialize(sw, playlistItem); + xw.WriteStartDocument(); + xw.WriteStartElement("MPData"); + xw.WriteStartElement("Playlist"); + xw.WriteAttributeString("type", playlistType); - //write serialisation to xmltextwriter, without XML declaration line - xw.WriteRaw(sb.ToString().Substring(sb.ToString().IndexOf("?>") + 2)); + for (int i = 0; i <= playlist.Count - 1; i++) + { + playlistItem = (MediaPortal.Playlists.PlayListItem)playlist[i]; + xs.Serialize(xw, playlistItem); + } - //clear serialisation stringbuilder - sb.Remove(0, sb.Length - 1); + xw.WriteEndElement(); + xw.WriteEndElement(); + xw.WriteEndDocument(); + + xw.Flush(); } + catch (Exception ex) + { + return ex.ToString(); + } - xw.WriteEndElement(); - xw.WriteEndElement(); - xw.WriteEndDocument(); - - return sbXML.ToString(); + return System.Text.Encoding.UTF8.GetString(xwMem.ToArray()); } [WebMethod(Description="This command adds an item to the nominated playlist.")] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |