📄 sample37.cs
字号:
namespace apiBook
{
using System;
using System.Xml;
using System.IO;
using System.Collections;
public class TestXmlNodeClass
{
public static void Main()
{
XmlDocument testXD = new XmlDocument(); testXD.Load("C:\\students.xml");
Console.WriteLine("所有学生资料:");
XmlNode testXN = testXD.DocumentElement;
IEnumerator testIE = testXN.GetEnumerator();
//使用GetEnumerator方法获取对XmlNode中节点上“for each”样式迭代的支持
XmlNode tempXN;
while (testIE.MoveNext())
{
tempXN = (XmlNode) testIE.Current;
Console.WriteLine(tempXN.OuterXml); }
Console.WriteLine("将Mike资料装载进来:");
testXD.LoadXml("<student id='1' xmlns:sn='scut:fresh' sn:sex='male'>"+" <name>Mike</name>"+"<dept>Computer</dept>" +"<country>China</country>"+"</student>");
testXD.Save(Console.Out);
Console.WriteLine();
testXN = testXD.FirstChild;
string str = testXN. GetNamespaceOfPrefix("sex");
//使用GetNamespaceOfPrefix方法查找当前节点范围内离给定的前缀最近的xmlns声明
XmlNode addXN = testXD.CreateNode(XmlNodeType.Attribute, "age", str);
addXN.Value = "22";
testXN.Attributes.SetNamedItem(addXN);
Console.WriteLine("添加属性sex后,该内容为:");
testXD.Save(Console.Out);
Console.WriteLine();
testXN = testXD.FirstChild;
string pfStr = testXN.GetPrefixOfNamespace("male");
//使用GetPrefixOfNamespace方法查找当前节点范围内离给定的命名空间URI最近的xmlns声明
XmlElement testXE = testXD.CreateElement(pfStr, "style", "simple Chinese");
testXE.InnerText = "test";
testXN.AppendChild(testXE);
Console.WriteLine("添加新元素后XML文档内容:");
testXD.Save(Console.Out);
Console.WriteLine();
Console.ReadLine();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -