创建XML文档
1.使用XMLDocument的方式
XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null)); XmlElement newbook = doc.CreateElement("book"); newbook.SetAttribute("genre", "Mystery"); newbook.SetAttribute("publicationdate", "2001"); newbook.SetAttribute("ISBN", "123345525"); XmlElement newTitle = doc.CreateElement("title"); newTitle.InnerText = "The Case of The missing cookie"; newbook.AppendChild(newTitle); XmlElement newAuthor = doc.CreateElement("Author"); newAuthor.InnerText = "James Lorain"; newbook.AppendChild(newAuthor); if (doc.DocumentElement == null) doc.AppendChild(newbook); XmlTextWriter tr = new XmlTextWriter("newbook.xml", Encoding.UTF8); tr.Formatting = Formatting.Indented; doc.WriteContentTo(tr); tr.Close();
2.使用XDocument的方式
XDocument xdoc = new XDocument(); XElement root = new XElement("Book"); xdoc.Add(root); XAttribute genre = new XAttribute("genre", "Mystery"); XAttribute date = new XAttribute("publicationdate", "2001"); XAttribute isbn = new XAttribute("ISBN", "123345525"); root.Add(genre, date, isbn); XElement title = new XElement("title"); title.Value = "The Case of The missing cookie"; XElement author = new XElement("Author", "James Lorain"); root.Add(title, author); xdoc.Save("xdoc.xml");
2.XDocument的简洁方式
var doc = new XDocument(new XElement("Book", new XAttribute("genre", "Mystery"), new XAttribute("publicationdate", "2002"), new XAttribute("ISBN", "123345525"), new XElement("title", "The Case of The missing cookie"), new XElement("Author", "James Lorain") ) ); doc.Save("xdoc2.xml");
查询
查找所有<PERSONA>元素,并且打印其值
XDocument doc = XDocument.Load("hamlet.xml"); doc.Descendants("PERSONA").Select(e => e.Value).ToList().ForEach(s=> { Console.WriteLine(s); }) ;
查找<PLAY>元素下面<TILTE>元素,打印其值
<PLAY>
<TITLE>The Tragedy of Hamlet, Prince of Denmark</TITLE>
XDocument doc = XDocument.Load("hamlet.xml"); Console.WriteLine(doc.Element("PLAY").Element("TITLE").Value);
Element("****")找到第一个符合要求的元素。
对于下面XML文件,
查找root下面长度为5的元素,打印其名称,2个属性的名称
<?xml version="1.0" encoding="utf-8"?> <root color="red" size="large"> <first color="red" size="medium" /> <second color="green" size="large" /> <third color="yellow" size="small" /> </root>
XDocument.Load("xdoc2.xml").Root.Elements().Where(e => e.Name.ToString().Length == 5).ToList().ForEach( e => Console.WriteLine($"Element Name:{e.Name}, Color:{e.Attribute("color")},Size: {e.Attribute("size")}"));
结果
Element Name:first, Color:color="red",Size: size="medium"
Element Name:third, Color:color="yellow",Size: size="small"
查找属性color为green的元素,然后如上面打印
XDocument.Load("xdoc2.xml").Root.Elements().Where(e => e.Attribute("color").Value=="green").ToList().ForEach( e => Console.WriteLine($"Element Name:{e.Name}, Color:{e.Attribute("color")},Size: {e.Attribute("size")}"));
枚举Root下面的 元素,投影到新的对象集合,并且打印
XDocument.Load("xdoc2.xml").Root.Elements().Select(e => new { name=e.Name, color = e.Attribute("color").Value }) .ToList().ForEach(e => Console.WriteLine($"Name: {e.name} And Color {e.color}"));
结果
Name: first And Color red
Name: second And Color green
Name: third And Color yellow
修改
在元素PLAY下面的元素PERSONAE中添加一个元素PERSONA
XElement xe = new XElement("PERSONA", "We The People"); doc.Element("PLAY").Element("PERSONAE").Add(xe);
修改某一个元素的值
doc.Element("PLAY").Element("PERSONAE").Element("PERSONA").SetValue("KING III");
要保存到文件,最后需要doc.Save
一个添加节点的例子
<?xml version="1.0" encoding="utf-8"?> <Root> <Student id="1"> <name>Tom</name> <age>18</age> </Student> <Student id="2"> <name>Jim</name> <age>20</age> </Student> </Root>
要添加一个Student元素,
var content = new XElement("Student", new XAttribute("Id", "3"), new XElement("name", "Jack"), new XElement("age", "22"), new XElement("sex", "gay") ); var doc = XDocument.Load("xdoc2.xml"); doc.Element("Root").LastNode.AddAfterSelf(content);