C# XML,XmlDocument简单操作实例

     private static string _Store = LocalPathHelper.CurrentSolutionPath + "/data/bookstore.xml";

1.添加节点

/// <summary>
/// 向根节点中插入一个节点
/// </summary>
public static void AddOne()
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(_Store);

    //1.查找booksotre节点
    XmlNode root = xmlDoc.SelectSingleNode("bookstore");
    //2.创建book 节点
    XmlElement book = xmlDoc.CreateElement("book");
    book.SetAttribute("genre", "lizanhong");
    book.SetAttribute("ISBN", "2-3431-4");
    XmlElement title = xmlDoc.CreateElement("title");
    title.InnerText = "C#入门经典";
    book.AppendChild(title);
    XmlElement author = xmlDoc.CreateElement("author");
    author.InnerText = "厚街";
    book.AppendChild(author);
    XmlElement price = xmlDoc.CreateElement("price");
    price.InnerText = "58.3";
    book.AppendChild(price);

    //将book节点,添加到根节点
    root.AppendChild(book);

    //保存内容
    xmlDoc.Save(_Store);
}

2.修改节点

/// <summary>
/// 修改节点
/// </summary>
public static void UpdateOne()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);
    //遍历修改
    XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;
    foreach (XmlNode node in nodeList)
    {
        //将子节点类型转换为XmlEletment类型
        XmlElement ele = (XmlElement)node;
        if (ele.GetAttribute("genre") == "lizanhong")
        {
            ele.SetAttribute("genre", "udpate礼赞红");
            XmlNodeList nodeList2 = ele.ChildNodes;
            foreach (XmlNode node2 in nodeList2)
            {
                XmlElement ele2 = (XmlElement)node2;
                if (ele2.Name == "author")
                {
                    ele2.InnerText = "延纳";
                    break;
                }
            }
            break;
        }
    }
    //保存修改
    doc.Save(_Store);
}
/// <summary>
/// 修改节点2,使用xpath
/// </summary>
public static void UpdateTwo()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);
    //查询节点
    //XmlNode root = doc.SelectSingleNode("bookstore");
    //XmlNodeList books = doc.SelectNodes("bookstore/book");
    XmlNode title = doc.SelectNodes("bookstore/book/title")[0];
    title.InnerText = title.InnerText + "---xpath";
    doc.Save(_Store);
}

3.删除节点

/// <summary>
/// 删除节点,属性,内容
/// </summary>
public static void DeleteOne()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);
    XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;
    foreach (var item in nodeList)
    {
        XmlElement ele = (XmlElement)item;
        if (ele.GetAttribute("genre") == "fantasy")
        {
            //删除属性
            ele.RemoveAttribute("genre");
        }
        else if (ele.GetAttribute("genre") == "udpate礼赞红")
        {
            //删除该节点的全部内容
            ele.RemoveAll();
        }
    }
    //保存修改
    doc.Save(_Store);
}
/// <summary>
/// 删除空节点
/// </summary>
public static void DeleteTwo()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);

    XmlNode root = doc.SelectSingleNode("bookstore");
    XmlNodeList nodeList = root.ChildNodes;
    foreach (XmlNode node in nodeList)
    {
        XmlElement ele = (XmlElement)node;
        if (ele.ChildNodes.Count <= 0)
            //只能删除直接子节点
            root.RemoveChild(node);
    }
    doc.Save(_Store);
}

4.查询列表

/// <summary>
/// 显示所有的数据
/// </summary>
public static void ShowOne()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);

    XmlNode root = doc.SelectSingleNode("bookstore");
    XmlNodeList nodeList = root.ChildNodes;
    foreach (var node in nodeList)
    {
        XmlElement ele = (XmlElement)node;
        Console.WriteLine(ele.GetAttribute("genre"));
        Console.WriteLine(ele.GetAttribute("ISBN"));
        XmlNodeList nodeList2 = ele.ChildNodes;
        foreach (XmlNode node2 in nodeList2)
        {
            Console.WriteLine(node2.InnerText);
        }
    }
}
时间: 2024-08-11 07:50:04

C# XML,XmlDocument简单操作实例的相关文章

行为驱动:Cucumber + Selenium + Java(一) - Cucumber简单操作实例

场景(Scenarios) 场景是Cucumber结构的核心之一.每个场景都以关键字“Scenario:”(或本地化一)开头,后面是可选的场景标题.每个Feature可以有一个或多个场景,每个场景由一个或多个步骤组成.一个非常简单的场景示例可以是: Scenario:验证帮助功能.给定用户导航到Facebook.当用户单击帮助时,将打开帮助页面. 考虑一种情况,其中我们需要不止一次地执行测试场景.假设,我们需要确保登录功能适用于所有类型的订阅用户.这需要多次执行登录功能场景.复制粘贴相同的步骤为

node.js下mongoose简单操作实例

Mongoose API : http://mongoosejs.com/docs/api.html // mongoose 链接var mongoose = require('mongoose');var db       = mongoose.createConnection('mongodb://127.0.0.1:27017/NodeJS'); // 链接错误db.on('error', function(error) {    console.log(error);}); // Sch

[转] node.js下mongoose简单操作实例

Mongoose API : http://mongoosejs.com/docs/api.html // mongoose 链接 var mongoose = require('mongoose'); var db = mongoose.createConnection('mongodb://127.0.0.1:27017/NodeJS'); // 链接错误 db.on('error', function(error) { console.log(error); }); // Schema 结

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.HtmlControl

MySQL基本概念以及简单操作

一.MySQL MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件. MySQL是将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性. MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化

简单操作XML

第一部分 什么是XML? XML, Extensible Markup Language ,可扩展标记语言,主要用途是描述和交换数据.它的一个用处是配置文件,用来保存数据库连接字符串.端口.IP.日志保存路径等参数.我们可以使用文本文件来保存文件,使用 key = value, key2 = value2 ,...... 的方式来保存数据.这样做的坏处是结构比较不规矩,读取起来也不方便,需要自行编写一长串的if / else 语句.为了解决这些问题,我们可以使用XML. XML定义了一组规则,即

Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)

一, GNU工具链简介: (1)编译代码步骤: 预处理 -> 编译 -> 汇编 -> 链接: 预处理:去掉注释,进行宏替换,头文件包含等工作: gcc -E test.c -o test.i 编译:   不同平台使用汇编语言不同,汇编将高级语言编译成汇编语言: gcc -S test.c -o test.s 汇编:   将汇编语言翻译成二进制代码: gcc -c test.c -o test.o 链接:   包含各函数库的入口,得到可执行文件: gcc -o test test.c (2

Linq To XML 简单操作

加载xml文件和保存xml文件 XDocument doc = XDocument.Load(Server.MapPath("\\xmlfile\\Book.xml")); doc.Save(Server.MapPath("\\xmlfile\\BookBackup.xml")); 创建元素 XElement xe = new XElement("book",new XElement("bookname","asp.

C#简单注册表操作实例

1.简介操作 //设置注册值 private void Button_Click(object sender, RoutedEventArgs e) { //路径及间隔符号要正确 //1.如果指定路径不存在,则创建 //2.如果指定键存在,则覆盖值 string path = "HKEY_CURRENT_USER\\myRegOne"; Registry.SetValue(path, "Expend", "hellow world!"); Mes