📄 sample18.cs
字号:
namespace apiBook
{
using System;
using System.Xml;
using System.IO;
public class TestClass
{
public static void Main()
{
XmlDocument testXD = new XmlDocument();
testXD.LoadXml("<student Id='1'>" +"<name>\"Rose\"</name>"+"</student>");
XmlAttribute testXA = testXD.CreateAttribute("dept");
testXA.Value = "Computer";
XmlAttributeCollection testXAC = testXD.DocumentElement.Attributes;
testXAC.Append(testXA);
//使用Append方法将指定的属性插入集合,并将其作为集合中的最后一个节点
Console.WriteLine(" 显示XML内容:"); Console.WriteLine(testXD.OuterXml);
XmlAttribute[] testXAA = new XmlAttribute[2];
int index=0;
testXAC.CopyTo(testXAA, index);
//使用CopyTo方法从该集合中将所有XmlAttribute对象复制到给定数组
Console.WriteLine("显示数组的属性:");
foreach (XmlAttribute xa in testXAA)
{
Console.WriteLine(xa.Name+"="+xa.Value);
}
testXA = testXD.CreateAttribute("major");
testXA.Value = "Software";
testXAC.InsertAfter(testXA, testXAC[1]);
//使用InsertAfter方法将指定属性直接插入到指定引用属性之后
Console.WriteLine("修改后的内容:");
Console.WriteLine(testXD.OuterXml);
Console.WriteLine("删除dept属性后的内容:");
testXAC.Remove(testXAC["dept"]);
//使用Remove方法从集合中移除指定的属性
Console.WriteLine(testXD.OuterXml);
Console.WriteLine("在属性name前面插入属性CId");
testXA = testXD.CreateAttribute("CId");
testXA.Value = "001";
testXAC.InsertBefore(testXA,testXAC[1]);
//使用InsertBefore方法将指定属性直接插入到指定引用属性之前
Console.WriteLine(testXD.OuterXml);
Console.WriteLine("在最后面添加属性GId");
testXA = testXD.CreateAttribute("GId");
testXA.Value = "03";
testXAC.SetNamedItem(testXA);
//使用SetNamedItem方法添加XmlNode对象
Console.WriteLine(testXD.OuterXml);
Console.WriteLine("在最前面添加属性School");
testXA = testXD.CreateAttribute ("School");
testXA.Value = "SCUT";
testXAC.Prepend(testXA);
//使用Prepend方法在最前面添加XmlNode对象
Console.WriteLine(testXD.OuterXml);
Console.WriteLine("移除第三个属性后内容:");
testXAC.RemoveAt(2);
//使用RemoveAt方法从集合中移除与指定的索引对应的属性
Console.WriteLine(testXD.OuterXml);
Console.ReadLine();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -