• xmlhelper


    XMLHelper.cs

    using System;
    using System.Data;
    using System.Xml;
    using System.Collections.Specialized;
    using System.Diagnostics;
    using System.Collections;


    namespace TMFHTong
    {
     /// <summary>
     /// Summary description for XmlHelper.
     /// </summary>
     public class XmlHelper
     {
      private XmlHelper()
      {
      }
            #region
            /*****************************************************************
             * Author: Payne Wee
             * Exemple:
             *   RockXML.List(path, "/Node", "name", "")
             *   RockXML.List(path, "/Node/Element[@Attribute='Name']", "name", "Attribute")
             *****************************************************************/
            public static DataTable List(string strPath, string strNode, string strAttributeKey, string strAttributeValue)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(strAttributeKey.ToUpper(), typeof(string));
                dt.Columns.Add((strAttributeValue == string.Empty ? "Value" : strAttributeValue).ToUpper(), typeof(string));

                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(strPath);
                    XmlNode xn = doc.SelectSingleNode(strNode);

                    foreach (XmlNode n in xn.ChildNodes)
                    {
                        DataRow dr = dt.NewRow();
                        dr[0] = strAttributeKey.Equals(string.Empty) ? string.Empty : n.Attributes[strAttributeKey].Value;
                        dr[1] = strAttributeValue.Equals(string.Empty) ? n.InnerText : n.Attributes[strAttributeValue].Value;
                        dt.Rows.Add(dr);
                    }
                }
                catch { }

                return dt;
            }

            public static bool Create(string xmlFileName, string rootNodeName, string version, string encoding)
            {
                bool isSuccess = false;
                try
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, null);
                    XmlNode root = xmlDoc.CreateElement(rootNodeName);
                    xmlDoc.AppendChild(xmlDeclaration);
                    xmlDoc.AppendChild(root);
                    xmlDoc.Save(xmlFileName);
                    isSuccess = true;
                }
                catch (Exception ex)
                {
                    throw ex; //这里可以定义你自己的异常处理
                }
                return isSuccess;
            }

            /*****************************************************************
             * Author: Payne Wee
             * Exemple:
             *   RockXML.Read(path, "/Node", "")
             *   RockXML.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
             *****************************************************************/
            public static string Read(string strPath, string strNode, string strAttribute)
            {
                string value = string.Empty;
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(strPath);
                    XmlNode xn = doc.SelectSingleNode(strNode);
                    value = (strAttribute.Equals(string.Empty) ? xn.InnerText : xn.Attributes[strAttribute].Value);
                }
                catch { }
                return value;
            }

            /*****************************************************************
             * Author: Payne Wee
             * Exemple:
             *   RockXML.Insert(path, "/Node", "Element", "", "Value")
             *   RockXML.Insert(path, "/Node", "Element", "Attribute", "Value")
             *   RockXML.Insert(path, "/Node", "", "Attribute", "Value")
             *****************************************************************/
            public static void Insert(string strPath, string strNode, string strElement, string strAttribute, string strValue)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(strPath);
                    XmlNode xn = doc.SelectSingleNode(strNode);
                    if (strElement.Equals(string.Empty))
                    {
                        if (!strAttribute.Equals(string.Empty))
                        {
                            XmlElement xe = (XmlElement)xn;
                            xe.SetAttribute(strAttribute, strValue);
                        }
                    }
                    else
                    {
                        XmlElement xe = doc.CreateElement(strElement);
                        if (strAttribute.Equals(string.Empty))
                            xe.InnerText = strValue;
                        else
                            xe.SetAttribute(strAttribute, strValue);
                        xn.AppendChild(xe);
                    }
                    doc.Save(strPath);
                }
                catch { }
            }

            /*****************************************************************
             * Author: Payne Wee
             * Exemple:
             *   RockXML.Modify(path, "/Node", "", "Value")
             *   RockXML.Modify(path, "/Node", "Attribute", "Value")
             *****************************************************************/
            public static void Modify(string strPath, string strNode, string strAttribute, string strValue)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(strPath);
                    XmlNode xn = doc.SelectSingleNode(strNode);
                    XmlElement xe = (XmlElement)xn;
                    if (strAttribute.Equals(string.Empty))
                        xe.InnerText = strValue;
                    else
                        xe.SetAttribute(strAttribute, strValue);
                    doc.Save(strPath);
                }
                catch { }
            }

            /*****************************************************************
             * Author: Payne Wee
             * Exemple:
             * RockXML.Delete(path, "/Node", "")
             * RockXML.Delete(path, "/Node", "Attribute")
             *****************************************************************/
            public static void Delete(string strPath, string strNode, string strAttribute)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(strPath);
                    XmlNode xn = doc.SelectSingleNode(strNode);
                    XmlElement xe = (XmlElement)xn;
                    if (strAttribute.Equals(string.Empty))
                        xn.ParentNode.RemoveChild(xn);
                    else
                        xe.RemoveAttribute(strAttribute);
                    doc.Save(strPath);
                }
                catch { }
            }
            #endregion
          
     }
    }

    调用:

    string filepath = Server.MapPath("~")+"/xml/HouseList.xml";
                XmlHelper.Create(filepath, "HouseList", "1.0", "utf-8");
                XmlHelper.Insert(filepath, "HouseList", "House", "HouseNumber", "123");
                XmlHelper.Insert(filepath, "HouseList/House[@HouseNumber='123']", "ViewDate", "", "2013-09-28 15:12:03");

                XmlHelper.Insert(filepath, "HouseList", "House", "HouseNumber", "456");
                XmlHelper.Insert(filepath, "HouseList/House[@HouseNumber='456']", "ViewDate", "", "2013-10-28 15:12:03");

                string value = XmlHelper.Read(filepath, "/HouseList/House[@HouseNumber='123']", "");

                XmlHelper.Modify(filepath, "/HouseList/House[@HouseNumber='123']/ViewDate", "", "2013-10-28 15:30:47");

  • 相关阅读:
    Android长方形图片生成正圆形,以及矩形图片生成圆角
    android应用编译失败 ResXMLTree_node size 类错误,以及 android studio 项目内搜索
    Android 不能返回 parent Activity 的问题
    如何让有物理键的手机在ActionBar始终显示更多菜单menu键
    Android抽屉效果 DrawerLayout 入门经验总结
    Android使用Home键后应用程序重启的问题
    汉王云名片识别(SM)组件开发详解
    [deviceone开发]-购物车的简单示例
    [DeviceOne开发]-白板的示例
    [DeviceOne开发]-地区选择
  • 原文地址:https://www.cnblogs.com/codeloves/p/3344304.html
Copyright © 2020-2023  润新知