读写XML

作者在 2008-04-11 13:33:10 发布以下内容

using System;
using System.Xml;
using System.Windows.Forms;

namespace HVS.Common
{
    public class ReadXML
    {
        public static string GetXMLValueToString(string AppKey)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").FirstChild;
                if (xElem1 != null)
                    return xElem1.OuterXml.Substring(7).Substring(0, xElem1.OuterXml.Length - 15);

            }
            catch
            {

            }
            return "";
        }

        public static int GetXMLValueToInt(string AppKey)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").FirstChild;
                if (xElem1 != null)
                    return Convert.ToInt32(xElem1.OuterXml.Substring(7).Substring(0, xElem1.OuterXml.Length - 15));

            }
            catch
            {

            }
            return 0;
        }

        public static DateTime GetXMLValueToDateTime(string AppKey)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").FirstChild;
                if (xElem1 != null)
                    return Convert.ToDateTime(xElem1.OuterXml.Substring(7).Substring(0, xElem1.OuterXml.Length - 15));
            }
            catch
            {

            }
            return DateTime.MinValue;
        }
        public static bool GetXMLValueToBool(string AppKey)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").FirstChild;
                if (xElem1 != null)
                    return Convert.ToBoolean(xElem1.OuterXml.Substring(7).Substring(0, xElem1.OuterXml.Length - 15));

            }
            catch
            {

            }
            return false;
        }
    }
}

using System;
using System.Xml;
using System.Windows.Forms;

namespace HVS.Common
{
    public class WriteXML
    {
        public static void SetXMLValue(string AppKey, string AppValue)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
                if (xElem1 != null)
                    xElem1.InnerXml = AppValue;
                xDoc.Save(Application.ProductName + ".exe.config");
            }
            catch
            {

            }
        }

        public static void SetXMLValue(string AppKey, int AppValue)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
                if (xElem1 != null)
                    xElem1.InnerXml = AppValue.ToString();
                xDoc.Save(Application.ProductName + ".exe.config");
            }
            catch
            {

            }
        }

        public static void SetXMLValue(string AppKey, bool AppValue)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
                if (xElem1 != null)
                    xElem1.InnerXml = AppValue.ToString();
                xDoc.Save(Application.ProductName + ".exe.config");
            }
            catch
            {

            }
        }

        public static void SetXMLValue(string AppKey, DateTime AppValue)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
                if (xElem1 != null)
                    xElem1.InnerXml = AppValue.ToString();
                xDoc.Save(Application.ProductName + ".exe.config");
            }
            catch
            {

            }
        }
    }
}

 

 

XML | 阅读 2035 次
文章评论,共0条
游客请输入验证码
浏览25388次