C# 操作XML

    public class KeyValue
    {
        private string key = string.Empty;

        public string Key
        {
            get { return key; }
            set { key = value; }
        }
        private string value = string.Empty;

        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        public KeyValue(string key, string value)
        {
            this.Key = key;
            this.Value = value;
        }
    }

    public class Result
    {
        public static string CreateResult(string status, string message, List<KeyValue> rsList)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点  
                XmlNode root = xmlDoc.CreateElement("data");
                xmlDoc.AppendChild(root);

                XmlNode result = xmlDoc.CreateNode(XmlNodeType.Element, "result", null);
                CreateNode(xmlDoc, result, "status", status);
                CreateNode(xmlDoc, result, "message", message);
                root.AppendChild(result);

                XmlNode writetext = xmlDoc.CreateNode(XmlNodeType.Element, "writetext", null);
                root.AppendChild(writetext);

                if (rsList != null)
                {
                    for (int i = 0; i < rsList.Count; i++)
                    {
                        CreateNode(xmlDoc, writetext, rsList[i].Key, rsList[i].Value);
                    }
                }
                return xmlDoc.InnerXml;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
            node.InnerText = value;
            parentNode.AppendChild(node);
        }

        public static XmlDocument CreateTitle(string status, string message)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点  
                XmlNode root = xmlDoc.CreateElement("data");
                xmlDoc.AppendChild(root);

                XmlNode result = xmlDoc.CreateNode(XmlNodeType.Element, "result", null);
                CreateNode(xmlDoc, result, "status", status);
                CreateNode(xmlDoc, result, "message", message);
                root.AppendChild(result);

                return xmlDoc;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string CreateList(XmlDocument xmlDoc, string elementParent, string item, List<KeyValue> rsList)
        {
            try
            {
                XmlNode xmlNode = xmlDoc.SelectSingleNode("data");
                XmlNode writetext = xmlNode.SelectSingleNode(elementParent);
                XmlNode itemNode = null;

                if (writetext == null)
                {
                    writetext = xmlDoc.CreateNode(XmlNodeType.Element, elementParent, null);
                    xmlNode.AppendChild(writetext);

                    itemNode = xmlDoc.CreateNode(XmlNodeType.Element, item, null);
                    writetext.AppendChild(itemNode);
                }
                else
                {
                    itemNode = xmlDoc.CreateNode(XmlNodeType.Element, item, null);
                    writetext.AppendChild(itemNode);
                }
                if (rsList != null)
                {
                    for (int i = 0; i < rsList.Count; i++)
                    {
                        CreateNode(xmlDoc, itemNode, rsList[i].Key, rsList[i].Value);
                    }
                }
                return xmlDoc.InnerXml;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

    public enum ResultState
    {
        success = 1, failed = 0, none = -1, error = -2, mserror = -3
    }

    public class HelperXml
    {
        /// <summary>
        /// 解析xml
        /// </summary>
        /// <param name="xmltext"></param>
        public static XmlNode AnalysisXml(string xmlfile)
        {
            try
            {
                System.Xml.XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlfile);

                XmlNode root = xmlDoc.SelectSingleNode("master");
                //获取根节点下所有子节点
                XmlNodeList nodelist = root.ChildNodes;
                return root;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取指定节点下所有节点
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public static XmlNode GetAppointXmlnode(XmlNode xmlNode, string nodeName)
        {
            try
            {
                XmlNode node = xmlNode.SelectSingleNode(nodeName);
                return node;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

 

posted @ 2016-03-01 17:49  如.若  阅读(285)  评论(0编辑  收藏  举报