using System.Xml; XmlDocument doc"> using System.Xml; XmlDocument doc">
117.info
人生若只如初见

如何在C#中操作XML数据

在C#中操作XML数据,你可以使用.NET框架自带的System.Xml命名空间。下面是一些基本的操作XML数据的示例:

  1. 读取XML数据

假设你有一个名为"sample.xml"的文件,其内容如下:


    
        John Doe
        20
    
    
        Jane Smith
        22
    

你可以使用以下代码来读取这个文件:

using System.Xml;

XmlDocument doc = new XmlDocument();
doc.Load("sample.xml");

XmlNode root = doc.DocumentElement;
foreach (XmlNode node in root.ChildNodes)
{
    if (node.NodeType == XmlNodeType.Element && node.Name == "student")
    {
        string id = node.Attributes["id"].Value;
        string name = node.SelectSingleNode("name").InnerText;
        int age = int.Parse(node.SelectSingleNode("age").InnerText);

        Console.WriteLine($"Student ID: {id}, Name: {name}, Age: {age}");
    }
}
  1. 创建XML数据

你可以使用XmlDocument类来创建新的XML文档:

XmlDocument doc = new XmlDocument();
doc.LoadXml("");

XmlNode root = doc.DocumentElement;

XmlNode studentNode = doc.CreateNode(XmlNodeType.Element, "student", null);
XmlAttribute idAttr = doc.CreateAttribute("id");
idAttr.Value = "https://www.yisu.com/ask/1";
studentNode.Attributes.Append(idAttr);

XmlNode nameNode = doc.CreateNode(XmlNodeType.Element, "name", null);
nameNode.InnerText = "John Doe";
studentNode.AppendChild(nameNode);

XmlNode ageNode = doc.CreateNode(XmlNodeType.Element, "age", null);
ageNode.InnerText = "20";
studentNode.AppendChild(ageNode);

root.AppendChild(studentNode);

这段代码会创建一个新的XML文档,其中包含一个名为"student"的元素,该元素具有"id"、"name"和"age"子元素。

  1. 修改XML数据

你可以使用XmlDocument类的SelectSingleNode和SelectNodes方法来查找和修改XML元素:

// 查找第一个名为"name"的元素
XmlNode nameNode = doc.SelectSingleNode("/students/student/name");
if (nameNode != null)
{
    nameNode.InnerText = "Jane Doe";
}

// 查找所有名为"age"的元素,并将它们的值增加1
XmlNodeList ageNodes = doc.SelectNodes("/students/student/age");
if (ageNodes != null)
{
    foreach (XmlNode ageNode in ageNodes)
    {
        int age = int.Parse(ageNode.InnerText);
        ageNode.InnerText = (age + 1).ToString();
    }
}

这段代码会查找第一个名为"name"的元素,并将其值更改为"Jane Doe"。然后,它会查找所有名为"age"的元素,并将它们的值增加1。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fee5fAzsNAAFfAw.html

推荐文章

  • C# NotifyIcon的使用

    在C#中,NotifyIcon类用于在系统托盘中显示一个图标,并允许您与用户交互。以下是NotifyIcon类的一些常见用法: 创建NotifyIcon对象: NotifyIcon notifyIcon = ...

  • C# 中的random.Next方法

    在C#中,Random.Next()方法用于生成一个随机整数。它有多个重载版本,可以接受不同的参数来定义随机数的范围。
    下面是Random.Next()方法的一些常用重载版本...

  • ExtractFilePath - C++ Builder

    ExtractFilePath is a function in C++ Builder that is used to extract the directory path from a given file path.
    Syntax:
    AnsiString ExtractFi...

  • C#四舍五入的4种方法

    在C#中,有多种方法可以实现四舍五入。以下是四种常用的方法: Math.Round() 函数:Math.Round() 函数可以将一个浮点数四舍五入到最接近的整数。该函数有多个重载...

  • C#读取XML文件的方法是什么

    在C#中,可以使用System.Xml命名空间下的类来读取XML文件。以下是一个简单的示例,展示了如何使用C#读取XML文件的内容: 首先,创建一个名为sample.xml的XML文件...

  • ExpandableListView的点击事件处理

    ExpandableListView 的点击事件处理可以通过以下步骤实现: 首先,在你的布局文件中添加一个 ExpandableListView。例如: 创建一个自定义的 BaseExpandableListA...

  • ExpandableListView的数据加载方式

    ExpandableListView是Android开发中常用的一个列表控件,它允许用户展开和折叠每个列表项以查看更详细的内容。数据加载方式通常涉及以下几个步骤: 准备数据源:...

  • 怎样优化ExpandableListView性能

    要优化ExpandableListView的性能,可以采取以下措施: 使用简单的布局:确保每个列表项的布局尽可能简单,以减少渲染时间。避免在列表项中使用复杂的嵌套布局或大...