一、概念及特征:

1. XML 指可扩展标记语言(Extensible Markup Language),用户可以自己定义标签。XML 被设计用来传输和存储数据,而 HTML 用于格式化并显示数据,并且HTML不能自定义标签。

2. XML 文档形成一种树结构, XML 文档必须包含根元素。该元素是所有其他元素的父元素。XML 文档中的元素形成了一棵文档树。这棵树从根部开始,并扩展到树的最底端。

3. XML中所有元素都必须有关闭标签, XML 必须正确地嵌套, XML 的属性值须加引号, XML 标签对大小写敏感。

二、基本格式示例:

<bookstore>

 <book category="CHILDREN">

  <title>Harry Potter</title>

  <author>J K. Rowling</author>

  <year>2005</year>

  <price>29.99</price>

 </book>

 </bookstore>

三、使用XMLDocument读写XML

protected void btnCreate_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("bookstore.xml"));
XmlNode root = xmlDoc.SelectSingleNode("bookstore");
XmlElement xe1 = xmlDoc.CreateElement("book");
xe1.SetAttribute("genre", "李赞红");
xe1.SetAttribute("ISBN", "2-3631-4");
XmlElement xesub1 = xmlDoc.CreateElement("title");
xesub1.InnerText = "CS从入门到精通";
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("author");
xesub2.InnerText = "候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("price");
xesub3.InnerText = "58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);
xmlDoc.Save(Server.MapPath("bookstore.xml"));
} protected void EditNodes_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("bookstore.xml"));
XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == "李赞红")
{
xe.SetAttribute("genre", "update李赞红");
XmlNodeList nls = xe.ChildNodes;
foreach (XmlNode xn1 in nls)
{
XmlElement xe2 = (XmlElement)xn1;
if (xe2.Name == "author")
{
xe2.InnerText = "亚胜";
break;
}
}
break;
}
}
xmlDoc.Save(Server.MapPath("bookstore.xml"));
} protected void btnDelete_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("bookstore.xml"));
XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == "fantasy")
{
xe.RemoveAttribute("genre");
}
else if (xe.GetAttribute("genre") == "update李赞红")
{
xe.RemoveAll();
}
}
xmlDoc.Save(Server.MapPath("bookstore.xml"));
}

四、使用LINQ to XML读写XML,LINQ to XML 最重要的优势是它与 Language-Integrated Query (LINQ) 的集成。

private void CreateXmlFile()
{ ///设置新的XML文件保存的地址
string xmlFilePath = Server.MapPath("Books.xml");
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "no"),
new XElement("Books",
new XElement("Book",
new XAttribute("ID", "104"), ///添加属性ID
new XElement("No", "0004"), ///添加元素No
new XElement("Name", "Book 0004"), ///添加元素Name
new XElement("Price", "300"), ///添加元素Price
new XElement("Remark", "This is a book 0004.") ///添加元素Remark
)
)
);
///保存为XML文件
doc.Save(xmlFilePath);
///显示XML文件的内容
Response.Write(doc);
///设置网页显示的形式为XML文件
Response.ContentType = "text/xml";
Response.End();
} private void AddXmlElement()
{ ///导入XML文件
string xmlFilePath = Server.MapPath("Books.xml");
XElement xe = XElement.Load(xmlFilePath);
///创建一个新的节点
XElement book = new XElement("Book",
new XAttribute("ID", "105"), ///添加属性ID
new XElement("No", "0005"), ///添加元素No
new XElement("Name", "Book 0005"), ///添加元素Name
new XElement("Price", "500"), ///添加元素Price
new XElement("Remark", "This is a book 0005.") ///添加元素Remark
);
///添加节点到文件中,并保存
xe.Add(book);
xe.Save(xmlFilePath);
///显示XML文件的内容
Response.Write(xe);
///设置网页显示的形式为XML文件
Response.ContentType = "text/xml";
Response.End();
} private void UpdateXmlElement()
{///导入XML文件
string xmlFilePath = Server.MapPath("Books.xml");
XElement xe = XElement.Load(xmlFilePath);
///查找被替换的元素
IEnumerable<XElement> element = from e in xe.Elements("Book")
where e.Attribute("ID").Value == "104"
select e;
///替换为新元素,并保存
if (element.Count() > 0)
{
XElement first = element.First();
///设置新的属性
first.SetAttributeValue("ID", "106");
///替换新的节点
first.ReplaceNodes(
new XElement("No", "0006"), ///添加元素No
new XElement("Name", "Book 0006"), ///添加元素Name
new XElement("Price", "600"), ///添加元素Price
new XElement("Remark", "This is a book 0006.") ///添加元素Remark
);
}
xe.Save(xmlFilePath);
///显示XML文件的内容
Response.Write(xe);
///设置网页显示的形式为XML文件
Response.ContentType = "text/xml";
Response.End();
} private void RemoveXmlElement()
{
///导入XML文件
string xmlFilePath = Server.MapPath("Books.xml");
XElement xe = XElement.Load(xmlFilePath);
///查找被删除的元素
IEnumerable<XElement> element = from e in xe.Elements()
where (string)e.Element("Name") == "Book 0006"
select e;
///删除指定的元素,并保存
if (element.Count() > 0) { element.First().Remove(); }
xe.Save(xmlFilePath);
///显示XML文件的内容
Response.Write(xe);
///设置网页显示的形式为XML文件
Response.ContentType = "text/xml";
Response.End();
}

最新文章

  1. T-SQL中的随机数
  2. MongoDB的安装 转
  3. 使用ContentResolver添加数据、查询数据
  4. 【项目经验】如何用TexturePacker &amp; Physicseditor开发游戏
  5. SQL Server 堆表与栈表的对比(大表)
  6. list 操作
  7. DataTable的名称要后设置
  8. R-CNN论文翻译
  9. Ubuntu安装Anaconda
  10. React Native运行原理解析
  11. SQL 查询当前时间
  12. [Swift]LeetCode29. 两数相除 | Divide Two Integers
  13. 移动端根据dpr适配
  14. python之正则表达式及RE模块
  15. Python中斐波那契数列的四种写法
  16. Django介绍(2)
  17. Tomcat的最大并发数
  18. lombok 注解使用
  19. m2014_c-&gt;c语言容器类工具列
  20. [置顶] Xposed也要热更新

热门文章

  1. fuse的write过程到底是怎么样的,128KB的buffer怎么用?
  2. Intel&#174; Media Server Studio Support
  3. [Selenium] 操作 警告框、提示框、确认框
  4. 漫谈WebQQ 协议
  5. java服务器端断点续传
  6. SpringBoot整合Ribbon注入RestTemplate实例找不到原因
  7. (水题)洛谷 - P1618 - 三连击(升级版)
  8. Unity3D将来时:IL2CPP(上)
  9. python实现汉诺塔(递归)
  10. April Fools Contest 2017 A