C # xml operations

Source: Internet
Author: User
1. xml file format
<?xml version="1.0" encoding="utf-8"?><userInfo>  <userName>sa</userName>  <userPwd>111111</userPwd></userInfo>
2. Key code 2.1 muserinfo. CS
//************************************** ******************************** // Assembly: XML operation // 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 operation {// <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 operation // 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> </summa Ry> //************************************ * ********************************** using system. XML; namespace XML operation {// <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 (xmlconten T); 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. se Lectsinglenode ("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 );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.