XML
XML基础<?xml version="1.0" encoding="utf-8"?>
<Books>
<Book>
<BookName date="a" author="x">b</BookName>
</Book>
</Books>
XML(Extensible Makeup Language)可扩展标记语言,用来存储数据,规范了数据的存储结构(为每一个数据界定了范围),以便更方便的操作数据。 文档声明<?xml version="1.0" encoding="utf-8"?>
每个XML文档都要包含文档声明,version当前只有两个值:1.0和1.1,但vs不支持1.1。1.1并没有给Windows平台上使用的XML做什么改进,而且World Wide Web Consortium(www.w3c.org)建议尽可能使用第一版。 根元素每个文档都要有且只有一个根元素 <Books>
</Books>
元素 XML元素包含一个开始标记,元素中的数据和结束标记 特性<BookName date="qeqe" author="wewr">qwe</BookName>
除了在元素体内存储数据之外,也可以在特性内存储数据,以键值对的形式存,特性值必须包含在单引号或双引号内。存储多个特性用空格分隔。 元素和特性并没有太大的区别,如果以后需要为数据添加更多信息,最好使用元素,特性更便于保存对文档的每一位用户而言无关紧要的信息。可以根据自己的爱好选择。 XML文档的各个组成部分成为节点——元素,元素内的数据,特性,文档声明都是XML文档的节点。 DOM操作 XML文档对象模型(Document Object Model,DOM) using System.Xml;
namespace 集合
{
class Program
{
static void Main(string[] args)
{
XmlDocument xDocument = new XmlDocument();//在内存中创建一个文档对象
XmlDeclaration xDeclaration = xDocument.CreateXmlDeclaration("1.0","utf-8",null);//创建声明
xDocument.AppendChild(xDeclaration);//把声明写到文档中
XmlElement xRoot = xDocument.CreateElement("Persons");//创建根节点xRoot
xDocument.AppendChild(xRoot); //把根节点xRoot添加到文档xDocument
XmlElement xPerson1 = xDocument.CreateElement("Person");//创建子节点xPerson1
xPerson1.SetAttribute("id","1");//给子节点xPerson添加特性
xRoot.AppendChild(xPerson1);//把子节点Person添加到根节点xRoot
XmlElement xName1 = xDocument.CreateElement("Name");////创建子节点xName1
xName1.InnerText = "Olive";//给子节点xPName添加数据Olive
xPerson1.AppendChild(xName1);//给xPerson1添加节点xName1
XmlElement xAge1 = xDocument.CreateElement("Age");
xAge1.InnerText = "22";
xPerson1.AppendChild(xAge1);
XmlElement xPerson2 = xDocument.CreateElement("Person");
xPerson2.SetAttribute("id","2");
xRoot.AppendChild(xPerson2);
XmlElement xName2 = xDocument.CreateElement("Name");
xName2.InnerText = "Jenny";
xPerson2.AppendChild(xName2);
XmlElement xAge2 = xDocument.CreateElement("Age");
xAge2.InnerText = "23";
xPerson2.AppendChild(xAge2);
XmlElement xPerson3 = xDocument.CreateElement("Person");
xPerson3.SetAttribute("id","3");
xRoot.AppendChild(xPerson3);
XmlElement xName3 = xDocument.CreateElement("Name");
xName3.InnerText = "Danny";
xPerson3.AppendChild(xName3);
XmlElement xAge3 = xDocument.CreateElement("Age");
xAge3.InnerText = "24";
xPerson3.AppendChild(xAge3);
xDocument.Save("Persons.xml");//保存文件到硬盘
}
}
}
/* <?xml version="1.0" encoding="utf-8"?> <Persons> <Person id="1"> <Name>Olive</Name> <Age>22</Age> </Person> <Person id="2"> <Name>Jenny</Name> <Age>23</Age> </Person> <Person id="3"> <Name>Danny</Name> <Age>24</Age> </Person> </Persons> **/
Linq to XML 命名空间:System.Xml.Linq using System.Xml.Linq;
namespace 集合
{
class Program
{
static void Main(string[] args)
{
XDocument xDocument = new XDocument(
new XElement("Persons",new XElement("Person",new XAttribute("id","1"),new XElement("Name","Olive"),new XElement("Age","22")),"2"),"Jenny"),"23")),"3"),"Denny"),"24"))
));
xDocument.Save("Person.xml");
}
}
}
/*
<?xml version="1.0" encoding="utf-8"?>
<Persons>
<Person id="1">
<Name>Olive</Name>
<Age>22</Age>
</Person>
<Person id="2">
<Name>Jenny</Name>
<Age>23</Age>
</Person>
<Person id="3">
<Name>Danny</Name>
<Age>24</Age>
</Person>
</Persons>
**/
XElement.Add() XElement.AddFirst() XElement.Remove()方法或者XElement.RemoveAll() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |