C# XML 操作

來源:互聯網
上載者:User

標籤:style   blog   color   os   檔案   2014   

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);        }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.