用 XmlDocument 类修改和保存 XML

用 XmlDocument 类修改和保存 XML

概要
此示例演示如何使用 XmlDocument 类更新和保存 XML。

要求

下表概括了推荐使用的硬件、软件、网络架构以及所需的 Service Pack:
  • Microsoft Windows 2000 Professional、Windows 2000 Server、Windows 2000 Advanced Server 或 Windows NT 4.0 Server
  • Microsoft Visual Studio .NET
本文假定您熟悉下列主题:
  • XML 术语
  • 创建和读取 XML 文件
  • 文档对象模型 (DOM)

如何使用 XmlDocument 类保存 XML

  1. 在 Visual Studio .NET 中新建 Visual Basic 或 C# 控制台应用程序。
  2. 确保该项目引用 System.Xml 名称空间。
  3. Xml 名称空间上使用 Imports 语句,这样,以后就不需要在代码中限定 XmlTextReader 声明了。Imports 语句必须位于任何其他声明之前。 Visual Basic .NET 代码
    Imports System.Xml
    C# 代码
    using System.Xml;
  4. 新建 XmlDocument 类,然后使用 Load 方法加载它。

    XmlDocument 类表示 XML 文档并且使用 Load 方法从文件、流或 XmlReader 加载文档。

    Visual Basic .NET 代码
    Dim myXmlDocument as XmlDocument = new XmlDocument()
    myXmlDocument.Load ("books.xml"))
    C# 代码
    XmlDocument myXmlDocument = new XmlDocument();
    myXmlDocument.Load ("books.xml");
    请注意,尽管在此处使用了 Books.xml 文件,但是您可创建自己的 Books.xml 文件。Books.xml 示例文件还包括在 Visual Studio .NET 和 .NET 框架软件开发工具包 (SDK) 中。
  5. XmlNode 对象提供了操作节点的方法和属性。 使用 XmlDocument DocumentElement 属性返回的 XmlNode 对象操作 XML 节点。 Visual Basic .NET 代码
    Dim node as XmlNode
    node = myXmlDocument.DocumentElement
    C# 代码
    XmlNode node;
    node = myXmlDocument.DocumentElement;
  6. 迭代文档元素的子元素,并查找所有“price”节点。 对 Node 对象的 ChildNodes 属性使用 For Each 循环结构,查找所有 Name 属性等于“price”的节点。 将书籍的价格加倍。 Visual Basic .NET 代码
    Dim node2 As XmlNode 'Used for internal loop.
    Dim nodePriceText As XmlNode
    For Each node In node.ChildNodes
       'Find the price child node.
       For Each node2 In node.ChildNodes
          If node2.Name = "price" Then
             '                    nodePriceText = node2.InnerText
             Dim price As Decimal
             price = System.Decimal.Parse(node2.InnerText)
    
             ' Double the price.
             Dim newprice As String
             newprice = CType(price * 2, Decimal).ToString("#.00")
             Console.WriteLine("Old Price = " & node2.InnerText & Strings.Chr(9) & "New price = " & newprice)
             node2.InnerText = newprice
    End If
    Next
    Next
    C# 代码
    foreach(XmlNode node1 in node.ChildNodes)
       foreach (XmlNode node2 in node1.ChildNodes)
          if (node2.Name == "price")
             {
                Decimal price = Decimal.Parse(node2.InnerText);
                // Increase all the book prices by 20%
                String newprice = ((Decimal)price*(new Decimal(1.20))).ToString("#.00");
                Console.WriteLine("Old Price = " + node2.InnerText + "/tNew price = " + newprice);
                node2.InnerText = newprice;
              }
  7. 使用 XmlDocument 类的 Save 方法将修改后的 XML 保存到名为 InflatedPriceBooks.xml 的新文件中。

    可以使用 Save 方法将 XML 数据保存到文件、流和 XmlWriters 中。 Visual Basic .NET 代码
    myXmlDocument.Save("InflatedPriceBooks.xml"))
    C# 代码
    myXmlDocument.Save("InflatedPriceBooks.xml");
  8. 生成并运行您的项目
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值