利用XmlDocument操作XML文件
利用XmlDocument可以方便的操作XML文件。
1.操作XML文件基本方法
(1)添加对System.Xml的引用,并使用using语句添加引用;
(2)假设要读取的XML文件如下:
<?xml version="1.0" encoding="utf-8"?>
<Students>
<Student>
<Name>张靓靓</Name>
<Age>20</Age>
<Hobby>唱歌歌</Hobby>
</Student>
<Student>
<Name>周杰杰</Name>
<Age>22</Age>
<Hobby>耍双节棍棍</Hobby>
</Student>
</Students>
(3)编写程序,增加节点:
private static void AddNode()
{
XmlDocument myXml = new XmlDocument();
myXml.Load("Student.xml"); //读取指定的XML文档
XmlNode students = myXml.SelectSingleNode("Students");//读取Students节点
XmlElement newStudent = myXml.CreateElement("Student");
newStudent.SetAttribute("班级", "10软件");
XmlElement name = myXml.CreateElement("Name");
name.InnerText = "张三";
XmlElement age = myXml.CreateElement("Age");
age.InnerText = "20";
XmlElement hobby = myXml.CreateElement("Hobby");
hobby.InnerText = "看书";
newStudent.AppendChild(name);
newStudent.AppendChild(age);
newStudent.AppendChild(hobby);
students.AppendChild(newStudent);
myXml.Save("Student.xml");
}
(4)删除节点:
private static void DelNode()
{
XmlDocument myXml = new XmlDocument();
myXml.Load("Student.xml"); //读取指定的XML文档
XmlNode student= myXml.SelectSingleNode("//Student[Name='张德友']");//读取节点
if(student!=null)
student.ParentNode.RemoveChild(student);
myXml.Save("Student.xml");
}
(5)修改节点的值:
private static void ChangeNode()
{
XmlDocument myXml = new XmlDocument();
myXml.Load("Student.xml"); //读取指定的XML文档
XmlNodeList students = myXml.DocumentElement.ChildNodes;
foreach (XmlNode student in students)
{
XmlElement name=student.SelectSingleNode("Name") as XmlElement;//读取节点
if (name.InnerText.Equals("张三"))
name.InnerText = "李四";
}
myXml.Save("Student.xml");
}
(6)读取XML文件:
private static void ReadNode()
{
XmlDocument myXml = new XmlDocument();
myXml.Load("Student.xml"); //读取指定的XML文档
XmlNodeList students = myXml.DocumentElement.ChildNodes;//读取所有Student节点
foreach (XmlNode student in students)// 对子节点进行循环
{
foreach (XmlNode node in student.ChildNodes)
{
//将每个节点的内容显示出来
switch (node.Name)
{
case "Name":
Console.WriteLine("姓名:{0}", node.InnerText);
break;
case "Age":
Console.WriteLine("年龄:{0}", node.InnerText);
break;
case "Hobby":
Console.WriteLine("爱好:{0}", node.InnerText);
break;
}
}
}
}
2 操作ASP.NET网站站点地图文件:
站点地图文件Web.sitemap的格式如下:
<?xml version="1.0" encoding="utf-8"?>
<siteMapNode url="default.aspx" title="新书推荐" description="">
<siteMapNode title=" C#" url="BookList.aspx?typeid=1" description="" />
<siteMapNode title=".NET" url="BookList.aspx?typeid=25" description="" />
</siteMapNode>
/// <summary>
/// 更新全部XML
/// </summary>
private void ModifyXml()
{
xmldocTree.Load(Server.MapPath(CATAGORYXML));
xmldocTree.SelectSingleNode("siteMapNode").InnerText="";
foreach (Category catagory in CategoryManager.GetAllCategories())
{
MakeChild(catagory);
}
xmldocTree.Save(Server.MapPath(CATAGORYXML));
}
/// <summary>
/// 设置子节点
/// </summary>
/// <param name="catagory"></param>
private void MakeChild(Category catagory)
{
XmlElement xmlelNode = xmldocTree.CreateElement("siteMapNode");
xmlelNode.SetAttribute("title", catagory.Name);
xmlelNode.SetAttribute("url", BOOKLISTURL + catagory.Id.ToString());
xmlelNode.SetAttribute("description", "");
xmldocTree.SelectSingleNode("siteMapNode").AppendChild(xmlelNode);
}