/// <summary> /// 播放列表xml 转换成对象 /// </summary> /// <param name="xmlPlayList">xml结构的播放列表</param> /// <returns></returns> private static AMS_PlayList Parse(XmlDocument xmlPlayList) { AMS_PlayList plm = new AMS_PlayList(); XmlDocument xmlDoc = xmlPlayList; try { //查找根节点 XmlNode node = xmlDoc.SelectSingleNode("//Root"); plm.Number = node.Attributes["Num"].Value; //列表编号 plm.EffectDate = DateTime.Parse(node.Attributes["EffectDate"].Value); //生效日期 //plm.PlayElapsed = int.Parse(node.Attributes["PlayElapsed"].Value); plm.EndDate = DateTime.Parse(node.Attributes["EndDate"].Value); XmlNodeList nodes = xmlDoc.SelectNodes("//Root/VideoItems/Video"); //遍历找到的视频项 foreach (XmlNode n in nodes) { AMS_VideoItem item = new AMS_VideoItem(); item.Name = n.Attributes["name"].Value; item.PlayTime = n.Attributes["playtime"].Value; item.ReRelativeUrl = n.Attributes["source"].Value; item.MD5Value = n.Attributes["md5value"].Value; if (plm.PlayFileList.Count > 0) { plm.PlayFileList[plm.PlayFileList.Count - 1].SunTime = int.Parse((DateTime.Parse(item.PlayTime) - DateTime.Parse(plm.PlayFileList[plm.PlayFileList.Count - 1].PlayTime)).TotalSeconds.ToString().Split('.')[0]); } plm.PlayFileList.Add(item); //不重复的视频文件名称 int i; for (i = 0; i < plm.MediaFiles.Count; i++) { if (plm.MediaFiles[i].Name == item.Name) { break; } } if (i >= plm.MediaFiles.Count) { AMS_VideoItem Fileitem = new AMS_VideoItem(); Fileitem.Name = item.Name; Fileitem.ReRelativeUrl = item.ReRelativeUrl; Fileitem.MD5Value = item.MD5Value; plm.MediaFiles.Add(Fileitem); } } plm.PlayFileList[plm.PlayFileList.Count - 1].SunTime = int.Parse(node.Attributes["PlayElapsed"].Value); plm.PlayListTimeLength = int.Parse((DateTime.Parse(plm.PlayFileList[plm.PlayFileList.Count - 1].PlayTime) - DateTime.Parse(plm.PlayFileList[0].PlayTime)).TotalSeconds.ToString().Split('.')[0]) + plm.PlayFileList[plm.PlayFileList.Count - 1].SunTime; } catch (Exception ex) { throw ex; } return(plm); }
/// <summary> /// 播放列表xml 转换成对象 /// </summary> /// <param name="xmlPlayList">xml结构的播放列表</param> /// <returns></returns> public static AMS_PlayList Parse(string xmlPlayList) { AMS_PlayList plm = new AMS_PlayList(); XmlDocument xmlDoc = new XmlDocument(); try { //载入字符串类型的XML xmlDoc.LoadXml(xmlPlayList); plm = Parse(xmlDoc); } catch (Exception ex) { throw ex; } return(plm); }
/// <summary> /// 结构转换成XML /// </summary> /// <param name="playlist">播放列表</param> /// <returns></returns> public static string ToXml(AMS_PlayList playlist) { //TODO:转换成xml结构的算法 //创建一个xml对象 XmlDocument xmlDoc = new XmlDocument(); //创建开头 XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); xmlDoc.AppendChild(dec); //创建根节点 XmlElement root = xmlDoc.CreateElement("Root"); root.SetAttribute("Num", playlist.Number); root.SetAttribute("EffectDate", playlist.EffectDate.Value.ToShortDateString()); root.SetAttribute("EndDate", playlist.EndDate.Value.ToShortDateString()); root.SetAttribute("PlayElapsed", playlist.PlayFileList[playlist.PlayFileList.Count - 1].SunTime.ToString()); //创建二级节点 XmlElement SecNode = xmlDoc.CreateElement("VideoItems"); root.AppendChild(SecNode); //遍历媒体文件并添加到节点中 foreach (AMS_VideoItem video in playlist.PlayFileList) { XmlElement ThirdNode = xmlDoc.CreateElement("Video"); ThirdNode.SetAttribute("name", video.Name); ThirdNode.SetAttribute("playtime", video.PlayTime); ThirdNode.SetAttribute("source", video.ReRelativeUrl); ThirdNode.SetAttribute("md5value", video.MD5Value); SecNode.AppendChild(ThirdNode); //不重复的视频文件名称 } //在根节点中添加二级节点 root.AppendChild(SecNode); //添加根节点 xmlDoc.AppendChild(root); return(xmlDoc.OuterXml); }