首页 > 其他 > 详细

XML字符串解析实体类方法

时间:2016-08-26 01:22:43      阅读:285      评论:0      收藏:0      [点我收藏+]
/// <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字符串解析实体类方法

原文:http://www.cnblogs.com/mahuanpeng/p/5808769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!