DOM模式与LINQ操作XML简单对比
在这篇文章里,用XML DOM方式和LINQ方式对XML文档进行同样的操作,对比下有什么不同的,首先,生成如下格式的XML文档,
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<categories name="Technical">
<category name=".NET">
<books>
<book>CLR Via C#</book>
<book>Eessential .NET</book>
</books>
</category>
<category name="Design">
<books>
<book>Refacting</book>
<book>Head First Design Patterns</book>
</books>
</category>
</categories>
DOM方式:
XmlDocument doc = new XmlDocument();
XmlDeclaration declare = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
doc.AppendChild(declare);
XmlElement categories = doc.CreateElement("categories");
categories .SetAttribute("name", "Technical");
doc.AppendChild(categories);
XmlElement category = doc.CreateElement("category");
category.SetAttribute("name", ".NET");
categories.AppendChild(category);
XmlElement books = doc.CreateElement("books");
category.AppendChild(books);
XmlElement book = doc.CreateElement("book");
book.InnerText = "CLR Via C#";
books.AppendChild(book);
XmlElement book1 = doc.CreateElement("book");
book1.InnerText = "Eessential .NET";
books.AppendChild(book1);
XmlElement category1 = doc.CreateElement("category");
category1.SetAttribute("name", "Design");
categories.AppendChild(category1);
XmlElement books1 = doc.CreateElement("books");
category1.AppendChild(books1);
XmlElement book2 = doc.CreateElement("book");
book2.InnerText = "Refacting";
books1.AppendChild(book2);
XmlElement book3 = doc.CreateElement("book");
book3.InnerText = "Head First Design Patterns";
books1.AppendChild(book3);
LINQ方式:
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("categories", new XAttribute("name", "Technical"),
new XElement("category", new XAttribute("name", ".NET"),
new XElement("books",
new XElement("book", "CLR Via C#"),
new XElement("book", "Eessential .NET")
)
),
new XElement("category", new XAttribute("name", "Design"),
new XElement("books",
new XElement("book", "Refacting"),
new XElement("book", "Head First Design Patterns")
)
)
)
);
doc.Save("Data.xml");
LINQ方式生成的代码更接近XML的层次,简单类比图
查找元素
下面的代码找出有“.NET“属性的category节点下面的books节点的第二个book元素
XmlDocument doc = new XmlDocument();
if (System.IO.File.Exists("Data.xml"))
{
doc.Load("Data.xml");
}
XmlNode node = doc.DocumentElement.SelectSingleNode("/categories/category[@name='.NET']/books/book[position()=2]");
用LINQ查找节点内容是“Essential .NET “的元素的代码,
XDocument doc = new XDocument();
if (System.IO.File.Exists("Data.xml"))
{
doc = XDocument.Load("Data.xml");
}
XElement element = doc.Descendants("book").Where(book => (string)book == "Eessential .NET").First();
移除元素
DOM方式:
XmlDocument doc = new XmlDocument();
if (System.IO.File.Exists("Data.xml"))
{
doc.Load("Data.xml");
}
XmlNode node= doc.DocumentElement.SelectSingleNode("/categories/category[@name='.NET']/books");
node.RemoveChild(node.SelectSingleNode("book[position()=2]"));
doc.Save("Data.xml");
LINQ方式:
XDocument doc = new XDocument();
if (System.IO.File.Exists("Data.xml"))
{
doc = XDocument.Load("Data.xml");
}
doc.Descendants("book").Where(book => (string)book == "Eessential .NET").First().Remove();
doc.Save("Data.xml");
添加元素
DOM方式:
XmlDocument doc = new XmlDocument();
if (System.IO.File.Exists("Data.xml"))
{
doc.Load("Data.xml");
}
XmlNode node1 = doc.DocumentElement.SelectSingleNode("/categories/category[@name='.NET']/books");
XmlElement element = doc.CreateElement("book");
element.InnerText = "Eessential .NET";
node.AppendChild(element);
doc.Save("Data.xml");
LINQ方式:
XDocument doc = new XDocument();
if (System.IO.File.Exists("Data.xml"))
{
doc = XDocument.Load("Data.xml");
}
XElement parent = doc.Descendants("category").Where(pbook => pbook.Attribute("name").Value == ".NET").Elements("books").First();
XElement element = new XElement("book", "Eessential .NET");
parent.Add(element);
doc.Save("Data.xml");
修改元素内容与属性
DOM方式:
XmlDocument doc = new XmlDocument();
if (System.IO.File.Exists("Data.xml"))
{
doc.Load("Data.xml");
}
XmlNode node = doc.DocumentElement.SelectSingleNode("/categories/category[@name='.NET']/books/book[position()=2]");
node.InnerText = "IL Language";
XmlNode node1 = doc.DocumentElement.SelectSingleNode("/categories/category[@name='.NET']");
node1.Attributes["name"].Value = "DotNet";
doc.Save("Data.xml");
LINQ方式:
XDocument doc = new XDocument();
if (System.IO.File.Exists("Data.xml"))
{
doc = XDocument.Load("Data.xml");
}
XElement element = doc.Descendants("book").Where(book => (string)book == "Eessential .NET").First();
element.Value = "IL Language";
element.Parent.Parent.SetAttributeValue("name", "DotNet");
doc.Save("Data.xml");