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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    彻底解决SQL SERVER 2008无法远程连接的问题
    将ReportingService 2008配置为匿名访问
    将低版本的数据库迁移到sqlserver 2008
    Oracle 11G R2
    Reporting Services 安装的备份和还原操作
    DefaultValue
    用户 'IIS APPPOOL\DefaultAppPool' 登录失败。
    在IIS中为SQL Server 2008配置报表服务
    数据库日志维护方式
    如何卸载的 SQL Server 2008 实例
  • 原文地址:https://www.cnblogs.com/tianxue/p/3854037.html
Copyright © 2011-2022 走看看