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); } } }
C# XML 操作
时间: 2024-11-10 11:12:33