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);
        }

    }
}

C# XML 操作

时间: 2024-11-10 11:12:33

C# XML 操作的相关文章

XML操作

XML: XML 指可扩展标记语言 XML 被设计用来传输和存储数据. XML 被设计用来结构化.存储以及传输信息. xml文档展示: -----------------------------xml文档 <?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading

c#xml操作

简单的xml操作--解析技能xml xml文件 <skills> <skill> <id>1</id> <name lang="cn">大荒囚天指</name> <demage>100</demage> </skill> <skill> <id>2</id> <name lang="en">绝对零度</na

使用 IntraWeb (31) - IntraWeb 的 Xml 操作使用的是 NativeXml

在 IWNativeXml 单元. 知道了这个, 以后在其他 Delphi 程序中也可以直接 Uses IWNativeXml 了. TNativeXml (IWNativeXml.TNativeXml property AbortParsing: Boolean property BinaryEncoding: TBinaryEncodingType property CommentString: UTF8String property DropCommentsOnParse: Boolean

C#对一个XML操作的实用类

using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Data; using System.IO; namespace eBlog.Common.Files { public class XmlHelper { protected string strXmlFile; protected XmlDocument objXmlDoc = new XmlDoc

C#常用操作类库三(XML操作类)

/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { protected string strXmlFile; protected XmlDocument objXmlDoc = new XmlDocument(); public XmlHelper(string XmlFile) { // // TODO: 在这里加入建构函式的程序代码 // try { objX

php xml 操作。

参考 文章:http://www.cnblogs.com/zcy_soft/archive/2011/01/26/1945482.html DOMDocument相关的内容. 属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataType 返回此节点的数据类型 Definition 以DTD或XML模式给出的节点的定义(只读) Doctype 指定文档类型节点(只读) documentElement 返回文档的根元素(可读写) fi

C#:XML操作类

写的一个XML操作类,包括读取/插入/修改/删除. using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlContro

Python XML操作

XML(可扩展性标记语言)是一种非常常用的文件类型,主要用于存储和传输数据.在编程中,对XML的操作也非常常见. 本文根据python库文档中的xml.etree.ElementTree类来进行介绍XML的解析:https://docs.python.org/3.5/library/xml.etree.elementtree.html BTW,xml.etree.cElementTree模块从3.3以后就被弃用了. XML格式 首先,来看一下XML所包含的元素类型 1. 标签 <tag> 2.

xml 操作

//////////////////////////////////////////////////////////////////////////// 要操作的xml文件 <?xml version="1.0" encoding="GB2312" standalone="no"?><PhoneInfo>    <Brand name="华为">        <Type name=&