健康一贴灵,专注医药行业管理信息化

C# 读取XML示例

 

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace MAutoUpdate
{
    public class LocalInfo
    {
        public string LocalVersion { get; set; }
        public string LastUdpate { get; set; }
        public string ServerUpdateUrl { get; set; }
        public string LocalIgnoreVersion { get; set; }

        private string url = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Local.xml");


        public LocalInfo(string localAddress)
        {
            url = Path.Combine(localAddress, "Local.xml");
        }

        public void SaveReg(string subKey)
        {
            RegistryKey Key;
            Key = Registry.CurrentUser;
            //Key = Key.OpenSubKey("SOFTWARE\\GoodMES\\Update");
            Key = Key.OpenSubKey(subKey, true);

            foreach (var item in this.GetType().GetProperties())
            {
                Key.SetValue(item.Name.ToString(), this.GetType().GetProperty(item.Name.ToString()).GetValue(this, null).ToString());
            }
        }
        public void LoadReg(string subKey)
        {
            //获取本地配置文件
            RegistryKey Key;
            Key = Registry.CurrentUser;
            Key = Key.OpenSubKey(subKey);

            foreach (var item in this.GetType().GetProperties())
            {
                this.GetType().GetProperty(item.Name.ToString()).SetValue(this, Key.GetValue(item.Name.ToString()).ToString(), null);
            }
            Key.Close();
        }
        public void LoadXml()
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(url);
            var root = xdoc.DocumentElement;
            var listNodes = root.SelectNodes("/LocalUpdate");
            foreach (XmlNode item in listNodes)
            {
                RemoteInfo remote = new RemoteInfo();
                foreach (XmlNode pItem in item.ChildNodes)
                {
                    GetType().GetProperty(pItem.Name).SetValue(this, pItem.InnerText, null);
                }
            }
        }
        public void SaveXml()
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(url);
            var root = xdoc.DocumentElement;
            var listNodes = root.SelectNodes("/LocalUpdate");
            foreach (XmlNode item in listNodes)
            {
                foreach (XmlNode pItem in item.ChildNodes)
                {
                    // Key.SetValue(item.Name.ToString(), this.GetType().GetProperty(item.Name.ToString()).GetValue(this, null).ToString());
                    pItem.InnerText = this.GetType().GetProperty(pItem.Name.ToString()).GetValue(this, null).ToString();
                }
            }
            xdoc.Save(url);
        }
    }
}

 

<LocalUpdate>
  <LocalVersion>1.0.0.0</LocalVersion>
  <LocalIgnoreVersion>1.1.0.0</LocalIgnoreVersion>
  <LastUdpate>2016/9/28 9:25:24</LastUdpate>
  <ServerUpdateUrl>http://localhost/huidu/server.xml</ServerUpdateUrl>
</LocalUpdate>

 针对只有一个键值的XML文件,读取和修改键值程序示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace LRDDI
{
   public static class CommFunc
    {
        /// <summary>
        /// 获取XML文件中的节点信息(只限唯一键值)
        /// </summary>
        /// <param name="filename">XML文件名,须带扩展名**.xml</param>
        /// <param name="nodesString">节点信息,须带//,如:"//LocalUpdate"</param>
        /// <param name="keyString">键名</param>
        /// <returns></returns>
        public static string GetXmlValue(string filename,string nodesString,string keyString)
        {
            string valueString = "";
            try
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(filename);
                //XmlNode node = xmldoc.SelectSingleNode("//LocalUpdate");
                XmlNode node = xmldoc.SelectSingleNode(nodesString);
                valueString = node.SelectSingleNode(keyString).InnerText;
            }
            catch (Exception ex)
            {
                MessageBox.Show("取XML信息" + filename + "-" + nodesString + "出错!\r\n" + ex.Message); ;

            }
            return valueString;
        }

        /// <summary>
        /// 修改XML文件中的节点信息,(只限唯一键值)
        /// </summary>
        /// <param name="filename">XML文件名,须带扩展名**.xml</param>
        /// <param name="nodesString">节点信息,须带//,如:"//LocalUpdate"</param>
        /// <param name="keyString">键名</param>
        /// <param name="keyValue">键值</param>
        /// <returns></returns>
        public static string SetXmlValue(string filename, string nodesString, string keyString,string keyValue)
        {
            
            try
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(filename);
                XmlNode node = xmldoc.SelectSingleNode(nodesString);
                //设成新值
                node.SelectSingleNode(keyString).InnerText = keyValue;
                xmldoc.Save("config.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show("取XML信息" + filename + "-" + nodesString + "出错!\r\n" + ex.Message); ;
                return "0";
            }
            return "1";
        }
    }
}

 

posted @ 2022-08-09 09:30  一贴灵  阅读(165)  评论(0编辑  收藏  举报
学以致用,效率第一