zoukankan      html  css  js  c++  java
  • C# XML 操作

    1 xml文件格式

    <?xml version="1.0" encoding="utf-8"?>
    <userInfo>
      <userName>sa</userName>
      <userPwd>111111</userPwd>
    </userInfo>

    2 关键代码

    2.1 MUserInfo.cs

    // ***********************************************************************
    // Assembly         : XML操作
    // Author           : Amy
    // Created          : 02-17-2014
    //
    // Last Modified By : Amy
    // Last Modified On : 02-17-2014
    // ***********************************************************************
    // <copyright file="MUserInfo.cs" company="Microsoft">
    //     Copyright (c) Microsoft. All rights reserved.
    // </copyright>
    // <summary></summary>
    // ***********************************************************************
    
    namespace XML操作
    {
        /// <summary>
        /// Class MUserInfo.
        /// </summary>
        public class MUserInfo
        {
            /// <summary>
            /// The user name
            /// </summary>
            private string userName = string.Empty;
    
            /// <summary>
            /// The user password
            /// </summary>
            private string userPwd = string.Empty;
    
            /// <summary>
            /// Gets or sets the name of the user.
            /// </summary>
            /// <value>The name of the user.</value>
            public string UserName
            {
                get { return userName; }
                set { userName = value; }
            }
    
            /// <summary>
            /// Gets or sets the user password.
            /// </summary>
            /// <value>The user password.</value>
            public string UserPwd
            {
                get { return userPwd; }
                set { userPwd = value; }
            }
    
        }
    }

    2.2 XmlOperate.cs

    // ***********************************************************************
    // Assembly         : XML操作
    // Author           : Amy
    // Created          : 02-17-2014
    //
    // Last Modified By : Amy
    // Last Modified On : 02-18-2014
    // ***********************************************************************
    // <copyright file="XMLOperate.cs" company="Microsoft">
    //     Copyright (c) Microsoft. All rights reserved.
    // </copyright>
    // <summary></summary>
    // ***********************************************************************
    
    using System.Xml;
    
    namespace XML操作
    {
        /// <summary>
        /// Class XMLOperate.
        /// </summary>
        public class XMLOperate
        {
            /// <summary>
            /// Loads the XML.
            /// </summary>
            /// <param name="xmlfileName">Name of the xmlfile.</param>
            /// <returns>System.String.</returns>
            public static string LoadXML(string xmlfileName)
            {
                string result = string.Empty;
                XmlDocument xml = new XmlDocument();
                xml.Load(@xmlfileName);
                return xml.InnerXml.ToString();
            }
    
            /// <summary>
            /// Saves the XML.
            /// </summary>
            /// <param name="xmlContent">Content of the XML.</param>
            /// <param name="xmlFileName">Name of the XML file.</param>
            public static void SaveXML(string xmlContent, string xmlFileName)
            {
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(xmlContent);
                xml.Save(@xmlFileName);
            }
    
            /// <summary>
            /// Gets the user information by executable ml.
            /// </summary>
            /// <param name="fileName">Name of the file.</param>
            /// <returns>MUserInfo.</returns>
            public static MUserInfo GetUserInfoByXMl(string fileName)
            {
                MUserInfo model = new MUserInfo();
                XmlDocument xml = new XmlDocument();
                xml.Load(@fileName);
                XmlNode xmlUserName = xml.SelectSingleNode("userInfo/userName");
                if (xmlUserName != null)
                {
                    model.UserName = xmlUserName.InnerText.Trim();
                }
    
                XmlNode xmlUserPwd = xml.SelectSingleNode("userInfo/userPwd");
                if (xmlUserPwd != null)
                {
                    model.UserPwd = xmlUserPwd.InnerText.Trim();
                }
                return model;
            }
    
            /// <summary>
            /// Sets the user information automatic XML.
            /// </summary>
            /// <param name="flieName">Name of the flie.</param>
            /// <param name="model">The model.</param>
            public static void SetUserInfoToXML(string flieName, MUserInfo model)
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(@flieName);
                XmlNode xmlUserName = xml.SelectSingleNode("userInfo/userName");
                if (xmlUserName != null)
                {
                    xmlUserName.InnerText = model.UserName;
                }
    
                XmlNode xmlUserPwd = xml.SelectSingleNode("userInfo/userPwd");
                if (xmlUserPwd != null)
                {
                    xmlUserPwd.InnerText = model.UserPwd;
                }
    
                xml.Save(@flieName);
            }
    
        }
    }
    作者:BestNow
    出处:http://www.cnblogs.com/BestNow/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    SpringMVC项目模块浅析
    利用python脚本(xpath)抓取数据
    利用python脚本(re)抓取美空mm图片
    Intellij idea开发Hadoop MapReduce程序
    在Hadoop平台跑python脚本
    Hadoop常用命令
    CentOS6.5 安装Zookeeper集群
    MySQL常用命令
    MongoDB常用命令
    前后端分离架构+k8s+ingress
  • 原文地址:https://www.cnblogs.com/tianxue/p/3854037.html
Copyright © 2011-2022 走看看