//写入Xml 元素
string xmlFilePath = this.Server.MapPath("BookStore.xml"); //Xml 文件路径
XmlDocument xmlDoc = new XmlDocument(); //创建对象
xmlDoc.Load(xmlFilePath); //加载 XML 文件
XmlNode xmlNode = xmlDoc.SelectSingleNode("bookstore"); //选择节点
XmlElement xmlElem1 = xmlDoc.CreateElement("book"); //创建节点
xmlElem1.SetAttribute("address", "1234123412342"); //设置节点的属性
XmlElement xmlElem2 = xmlDoc.CreateElement("title"); //创建节点
xmlElem2.InnerText = "TitleTest"; //在节点插入文本
xmlElem1.AppendChild(xmlElem2); //添加到节点中
xmlElem2 = xmlDoc.CreateElement("author");
xmlElem2.InnerText = "authorTest";
xmlElem1.AppendChild(xmlElem2);
xmlElem2 = xmlDoc.CreateElement("price");
xmlElem2.InnerText = "priceTest";
xmlElem1.AppendChild(xmlElem2);
xmlNode.AppendChild(xmlElem1);
xmlDoc.Save(xmlFilePath); //保存 XML 文件
/******************************************************************/
/******************************************************************/
//修改 Xml 元素
string xmlFilePath = this.Server.MapPath("BookStore.xml"); //Xml 文件路路
XmlDocument xmlDoc = new XmlDocument(); //创建对象
xmlDoc.Load(xmlFilePath);
XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes; //获取节点下的子节点列表
foreach (XmlNode xn in xnl) //遍历所有节点
{
XmlElement xe = (XmlElement)xn; //把节点转换为元素
if (xe.GetAttribute("address") == "DelAll") //如果元素属性找到
{
xe.SetAttribute("address", "UpdateAbc"); //修改元素属性
XmlNodeList xnl2 = xe.ChildNodes; //获取它的子节点
foreach (XmlNode xn2 in xnl2) //遍历所有学点
{
if (xn2.Name == "title") //如果找到
{
xn2.InnerText = "UpdateAuthor"; //修改文本;
xmlDoc.Save(xmlFilePath);
break; //退出
}
}
break; //退出
}
}
/******************************************************************/
/******************************************************************/
//删除节点
string xmlFilePath = this.Server.MapPath("BookStore.xml"); //Xml 文件路路
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
int i = xnl.Count;
while (--i > -1)
{
XmlElement xe = (XmlElement)xnl[i];
if (xe.GetAttribute("address") == "Del")
xe.ParentNode.RemoveChild(xe);
}
xmlDoc.Save(xmlFilePath);
/******************************************************************/
/******************************************************************/
//读取 Xml
string xmlFilePath = this.Server.MapPath("BookStore.xml"); //Xml 文件路路
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
this.Response.Write("Address Title");
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
this.Response.Write("<br/>" + xe.GetAttribute("address"));
XmlNodeList xnl2 = xe.ChildNodes;
foreach (XmlNode xn2 in xnl2)
{
this.Response.Write(" " + xn2.InnerText);
}
//元素与节点的区别
//XmlElement 元素,元素有自身的属性、子节点
//XmlNode 节点,节点没有自身的属性、有子节点
//区别就是,元素有自身的属性,但节点没有自身的属性,它们都可以有子节点,其它基本相同
}
/******************************************************************/
/******************************************************************/
//创建 Xml 文件
string xmlFilePath = this.Server.MapPath("Test.xml"); //Xml 文件路路
XmlDocument xmlDoc = new XmlDocument(); //创建实例
XmlNode xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", ""); //新建节点
XmlElement xmlElem = xmlDoc.CreateElement("Test"); //新建元素
xmlDoc.AppendChild(xmlNode); //添加节点
xmlDoc.AppendChild(xmlElem); //添加元素
xmlNode = xmlDoc.SelectSingleNode("Test"); //选择节点
for (int i = 0; i < 3; i++) //循环添加节点
{
xmlElem = xmlDoc.CreateElement("Test1"); //创建元素
xmlElem.SetAttribute("name", "liuga"); //设置元素属性
XmlElement xmlElem1 = xmlDoc.CreateElement("Test11"); //创建元素
xmlElem1.InnerText = "Test111"; //设置文本
xmlElem.AppendChild(xmlElem1); //添加元素
xmlNode.AppendChild(xmlElem); //添加元素
}
xmlDoc.Save(xmlFilePath); //保存
/******************************************************************/
/******************************************************************/
string xmlFilePath = this.Server.MapPath("Test.xml"); //Xml 文件路路
XmlTextReader xtr = new XmlTextReader(xmlFilePath);
while (xtr.Read())
{
switch (xtr.NodeType)
{
case XmlNodeType.EndElement:
if (xtr.Name == "Test1")
this.Response.Write("<br/>");
break;
case XmlNodeType.Element:
this.Response.Write(xtr.GetAttribute("name"));
break;
case XmlNodeType.Text:
this.Response.Write(xtr.Value);
break;
}
}
xtr.Close();
dom.DocumentElement.GetElementsByTagName("student"); // 获取某个特定的元素
一般操作 Element 不操作 Node(包括空格等等)