|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?注册帐号
x
什么是XML?
XML是扩展标记语言的简写,是一种开发的文本格式。
.Net是如何处理XML的呢?
本文首先从读、写XML开始介绍。
有如下一段XML:
<?xml version="1.0" encoding="utf-8" ?>
<students>
<!--我是一段注释文字-->
<student name="张平">
<courses>
<course name="语文">
<teacherComment>
<![CDATA[这里是语文老师的批注]]>
</teacherComment>
</course>
<course name="数学">
<teacherComment>
<![CDATA[这里是数学老师的批注]]>
</teacherComment>
</course>
</courses>
</student>
</students>
1.如何使用XmlDocument读取Xml
用一段代码遍历所有Student,并打印Student的所有属性和子节点的值
using System;
using System.Xml;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string xmlFilePath = @"C:\Users\xujianan\Desktop\LearnXML.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
//使用xpath表达式选择文档中所有的student子节点
XmlNodeList studentNodeList = doc.SelectNodes("/students/student");
if (studentNodeList != null)
{
foreach (XmlNode studentNode in studentNodeList)
{
//通过Attributes获得属性名字为name的属性
string name = studentNode.Attributes["name"].Value;
Console.WriteLine("Student:" + name);
//通过SelectSingleNode方法获得当前节点下的courses子节点
XmlNode coursesNode = studentNode.SelectSingleNode("courses");
//通过ChildNodes属性获得courseNode的所有一级子结点
XmlNodeList courseNodeList = coursesNode.ChildNodes;
Console.WriteLine(courseNodeList.Count);
if (courseNodeList != null)
{
foreach (XmlNode courseNode in courseNodeList)
{
Console.Write("\t");
Console.Write(courseNode.Name);
Console.Write(courseNode.Attributes["name"].Value);
Console.Write("老师评语");
XmlNode teacherCommentNode = courseNode.FirstChild;
XmlCDataSection cdata = (XmlCDataSection)teacherCommentNode.FirstChild;
Console.WriteLine(cdata.InnerText.Trim());
}
}
}
}
Console.Write("\r\nPress any key to continue.......");
Console.Read();
}
}
}
XmlDocument本身是从XmlNode继承的,读Xml节点可以通过FirstChild,LastChild,或者NextSibling,PreviousSibling读取单个节点,或者通过ChildNodes读取所有子节点。还可以使用XPath表达式使用SelectNodes(string xpath)或者SelectSingleNode(string xpath)读取单个或者多个符合条件的节点。
2.通过XmlDocument编辑Xml
using System;
using System.Xml;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration tempXmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(tempXmlDeclaration);
XmlNode rootNode = xmlDoc.CreateElement("students");
XmlNode studentNode = xmlDoc.CreateElement("student");
XmlAttribute nameAttribute = xmlDoc.CreateAttribute("name");
nameAttribute.Value = "张同学";
studentNode.Attributes.Append(nameAttribute);
XmlNode coursesNode = xmlDoc.CreateElement("courses");
XmlNode courseNode1 = xmlDoc.CreateElement("course");
XmlAttribute courseNameAttr = xmlDoc.CreateAttribute("name");
courseNameAttr.Value = "语文";
courseNode1.Attributes.Append(courseNameAttr);
XmlNode teacherCommentNode = xmlDoc.CreateElement("teacherComment");
XmlCDataSection cdata = xmlDoc.CreateCDataSection("<font color=\"red\">这是语文老师的批注</font>");
teacherCommentNode.AppendChild(cdata);
courseNode1.AppendChild(teacherCommentNode);
coursesNode.AppendChild(courseNode1);
studentNode.AppendChild(coursesNode);
rootNode.AppendChild(studentNode);
xmlDoc.AppendChild(rootNode);
xmlDoc.Save(@"d:\test.xml");
Console.WriteLine("已保存Xml文档");
}
}
}
以上两段代码分别从读取XML文件和编辑XML文件的角度对XML相关知识进行了简要介绍。
|
|