创建Xml

  /// <summary>
        /// The xmldocName
        /// </summary>
        /// Creator:wangxiaomei
        /// Creation Date:2012-6-28 11:38
        /// Modifier:
        /// Last Modified:
        /// ----------------------------------------------------------------------------------------
        private readonly string xmldocName = "ImmEntitlementPostData.xml";

        /// <summary>
        /// Gets the content XML by vod.
        /// </summary>
        /// <param name="dictionary">
        /// The dictionary.
        /// </param>
        /// <returns>
        /// The data
        /// </returns>
        /// Creator:wangxiaomei
        /// Creation Date:2012-6-28 11:29
        /// Modifier:
        /// Last Modified:
        /// ----------------------------------------------------------------------------------------
        public string GetContentXmlByParam(Dictionary<string, string> dictionary)
        {
            var xmlDocument = new XmlDocument();
            try
            {
                xmlDocument.Load(this.xmldocName);
            }
            catch
            {
                new Exception();
                this.CreateXml(this.xmldocName);
                xmlDocument.Load(this.xmldocName);
            }

            XmlNode root = xmlDocument.DocumentElement;
            if (root == null || root.InnerXml.Count() == 0)
            {
                return string.Empty;
            }

            this.UpdateXml(xmlDocument, dictionary);
            return xmlDocument.InnerXml;
        }

        /// <summary>
        /// Creates the XML.
        /// </summary>
        /// <param name="xmldocName">
        /// The xmldoc Name.
        /// </param>
        /// Creator:wangxiaomei
        /// Creation Date:2012-6-28 10:50
        /// Modifier:
        /// Last Modified:
        /// ----------------------------------------------------------------------------------------
        public void CreateXml(string xmldocName)
        {
            var xmldoc = new XmlDocument();
            var xmldec = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
            xmldoc.AppendChild(xmldec);
            var xmlelem = xmldoc.CreateElement(string.Empty, "ott", string.Empty);
            xmldoc.AppendChild(xmlelem);
            var root = xmldoc.SelectSingleNode("ott");
            var xtitle = xmldoc.CreateElement("title");
            xtitle.InnerText = "Secret Garden Ep 2";
            root.AppendChild(xtitle);
            var xforetv_pkg_id = xmldoc.CreateElement("foretv_pkg_id");
            xforetv_pkg_id.InnerText = "PACK1204090000001517";
            root.AppendChild(xforetv_pkg_id);
            var xforetv_group_title_id = xmldoc.CreateElement("foretv_group_title_id");
            xforetv_group_title_id.InnerText = "PACK1204090000001517";
            root.AppendChild(xforetv_group_title_id);
            var xcategory_id = xmldoc.CreateElement("category_id");
            xcategory_id.InnerText = "15";
            root.AppendChild(xcategory_id);
            var xcontent_type = xmldoc.CreateElement("content_type");
            xcontent_type.InnerText = "vod";
            root.AppendChild(xcontent_type);
            var xpolicy_group_id = xmldoc.CreateElement("policy_group_id");
            xpolicy_group_id.InnerText = "504289";
            root.AppendChild(xpolicy_group_id);
            var xoffering_type = xmldoc.CreateElement("offering_type");
            xoffering_type.InnerText = "SVOD";
            root.AppendChild(xoffering_type);
            var xcontent_category_id = xmldoc.CreateElement("content_category_id");
            xcontent_category_id.InnerText = "6|9";
            root.AppendChild(xcontent_category_id);
            var xrental_period = xmldoc.CreateElement("rental_period");
            xrental_period.InnerText = "3:00:00";
            root.AppendChild(xrental_period);
            var xrelease_date = xmldoc.CreateElement("release_date");
            var date = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss");
            xrelease_date.InnerText = date;
            root.AppendChild(xrelease_date);
            var xlicense_start = xmldoc.CreateElement("license_start");
            xlicense_start.InnerText = date;
            root.AppendChild(xlicense_start);
            var xlicense_end = xmldoc.CreateElement("license_end");
            var enddate = DateTime.Now.AddDays(1).ToString("yyyy-MM-ddThh:mm:ss");
            xlicense_end.InnerText = enddate;
            root.AppendChild(xlicense_end);
            var xactors = xmldoc.CreateElement("actors");
            xactors.InnerText = "Ramesh|Li Lei";
            root.AppendChild(xactors);
            var xdirector = xmldoc.CreateElement("director");
            xdirector.InnerText = "Sean Koh";
            root.AppendChild(xdirector);
            var xproducers = xmldoc.CreateElement("producers");
            xproducers.InnerText = "Keith Ho|Desmond Kee";
            root.AppendChild(xproducers);
            var xmso_rating = xmldoc.CreateElement("mso_rating");
            xmso_rating.InnerText = "0";
            root.AppendChild(xmso_rating);
            var xtrack_id = xmldoc.CreateElement("track_id");
            xtrack_id.InnerText = string.Empty;
            root.AppendChild(xtrack_id);
            var xepisode_id = xmldoc.CreateElement("episode_id");
            xepisode_id.InnerText = string.Empty;
            root.AppendChild(xepisode_id);
            var xepisode_name = xmldoc.CreateElement("episode_name");
            xepisode_name.InnerText = string.Empty;
            root.AppendChild(xepisode_name);
            var xseries_id = xmldoc.CreateElement("series_id");
            xseries_id.InnerText = string.Empty;
            root.AppendChild(xseries_id);
            var xseries_season_id = xmldoc.CreateElement("series_season_id");
            xseries_season_id.InnerText = string.Empty;
            root.AppendChild(xseries_season_id);
            var xcharge_code = xmldoc.CreateElement("charge_code");
            xcharge_code.InnerText = "charegeCode1";
            root.AppendChild(xcharge_code);
            xmldoc.Save(xmldocName);
        }

        /// <summary>
        /// Updates the XML.
        /// </summary>
        /// <param name="xmlDocument">
        /// The xmlDocument
        /// </param>
        /// <param name="dictionary">
        /// The dictionary.
        /// </param>
        /// Creator:wangxiaomei
        /// Creation Date:2012-6-28 11:42
        /// Modifier:
        /// Last Modified:
        /// ----------------------------------------------------------------------------------------
        public void UpdateXml(XmlDocument xmlDocument, Dictionary<string, string> dictionary)
        {
            if (xmlDocument == null || dictionary.Count == 0)
            {
                return;
            }

            XmlNode root = xmlDocument.DocumentElement;
            if (root == null || root.InnerXml.Count() == 0)
            {
                return;
            }

            foreach (XmlNode innerNode in root.ChildNodes)
            {
                // ReSharper disable AccessToModifiedClosure
                foreach (var key in dictionary.Where(key => innerNode != null && innerNode.Name == key.Key))
                // ReSharper restore AccessToModifiedClosure
                {
                    innerNode.InnerText = key.Value;
                }
            }

            xmlDocument.Save(this.xmldocName);
        }

原文地址:https://www.cnblogs.com/mmnyjq/p/2569462.html