首页 > 代码库 > XML字符串解析实体类方法

XML字符串解析实体类方法

/// <summary>    /// XML字符串解析实体类方法    /// </summary>    public class StringXML    {        public StringXML() { }        public StringXML(string ver, string node)        {            this.version = int.Parse(ver);            this.nodeText = int.Parse(node);        }        /// <summary>        /// XML版本号        /// </summary>        public int version { get; set; }        /// <summary>        /// 节点Text        /// </summary>        public int nodeText { get; set; }        /// <summary>        /// 字段内容转化成xml格式字符串        /// </summary>        /// <param name="ver"></param>        /// <param name="node"></param>        /// <returns></returns>        public static string ConvertToXMLStr(string ver, string node)        {            XmlDocument xmlDoc = new XmlDocument();            XmlElement version = xmlDoc.CreateElement("version");            version.InnerText = ver;            xmlDoc.AppendChild(version);            XmlElement nodeText = xmlDoc.CreateElement("nodeText");            nodeText.InnerText = node;            xmlDoc.AppendChild(nodeText);            return xmlDoc.OuterXml;        }        /// <summary>        /// XML字符串解析成实体类对象        /// </summary>        /// <param name="str"></param>        /// <returns></returns>        public StringXML AnalysisXmlStr(string str)        {            StringXML strXML = new StringXML();            XmlDocument xmlDoc = new XmlDocument();            xmlDoc.LoadXml(str);            XmlNodeList nodeList = xmlDoc.ChildNodes;            foreach (XmlNode node in nodeList)            {                if (node.Name == "version")                {                    strXML.version = int.Parse(node.InnerText);                               }                else if(node.Name == "nodeText")                {                    strXML.nodeText = int.Parse(node.InnerText);                }            }            return strXML;        }    }

 

XML字符串解析实体类方法