如何开始使用 XML 和 LINQ:初学者指南。






4.65/5 (8投票s)
我们如何利用 LINQ 的强大功能处理 XML 数据。
引言
在本文中,我们将向您展示 LINQ 处理 XML 数据的一些好东西。
快速浏览
我假设您了解 XML。 因此,我不会介绍基本的 XML 元素、属性。 并且希望您了解在何处以及为何需要使用 XML。
让我们看看我们的示例 XML 数据
<Products>
<Product id="1">
<Name>Desktop Computer</Name>
<Stock>12</Stock>
<ModelNo>DC001</ModelNo>
<Price>20000</Price>
</Product>
<Product id="2">
<Name>Compuetr Mouse</Name>
<Stock>100</Stock>
<ModelNo>CM001</ModelNo>
<Price>800</Price>
</Product>
<Product id="3">
<Name>Printer</Name>
<Stock>10</Stock>
<ModelNo>PR001</ModelNo>
<Price>5900</Price>
</Product>
</Products>
现在,我们将研究 LINQ 如何对以下内容有所帮助
1) 创建新的 XML 文档
2) 查询 XML 文档
3) 添加/追加新节点
4) 删除特定节点
通过简单示例开始使用 LINQ
要使用 XDocument
、XElement
或 XAttribute
,我们需要在我们的代码中包含 System.Xml.Linq
命名空间。 我们将通过示例逐一查看所有要点。 让我们从 XDocument 开始。
示例 1:将未解析的 XML 字符串加载到 XDocument 中
string myXML = "<Products><Product>Laptop</Product><Product>Printer</Product><Product>Mouse</Product></Products>";
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(myXML);
输出:(xdoc
内容)
<Products>
<Product>Laptop</Product>
<Product>Printer</Product>
<Product>Mouse</Product>
</Products>
以上情况是我们有格式良好的 XML 数据时。 如果我们不了解 XML 数据怎么办? 如果 myXML
包含一些不正确的 xml 数据格式,则会发生 XmlException
。 在所有给定的示例中,我们有许多具有不同构造函数的选项。 我将仅介绍一些构造函数。 您可以参考 MSDN 获取有关所有可用构造函数的信息。
示例 2:在现有 XDocument 中添加节点
string myXML = "<Products><Product>Laptop</Product><Product>Printer</Product><Product>Mouse</Product></Products>";
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(myXML);
// Add one more Product in xdoc
xdoc.Element("Products").Add(new XElement("Product", "Mobile"));
输出:(xdoc
内容)
<Products>
<Product>Laptop</Product>
<Product>Printer</Product>
<Product>Mouse</Product>
<Product>Mobile</Product>
</Products>
如您所见,我们在 xml 列表中添加了一个新产品。 由于我们正在从字符串值解析 xml 数据。 我们还可以从文件加载 xml 数据。 XDocument.Load
是我们可以用来从文件加载 xml 数据的方法。
示例 3:在现有 XDocument 中的第一个位置添加节点
string myXML = "<Products><Product>Laptop</Product><Product>Printer</Product><Product>Mouse</Product></Products>";
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(myXML);
// Add one more Product at first position in xdoc
xdoc.Element("Products").AddFirst(new XElement("Product", "Mobile"));
输出:(xdoc
内容)
<Products>
<Product>Mobile</Product>
<Product>Laptop</Product>
<Product>Printer</Product>
<Product>Mouse</Product>
</Products>
现在,让我们举一个真实生活中的例子,获取产品名称且字符长度等于 6。 使用旧方法,我们通常迭代所有元素并检查长度是否为 6。 但是现在借助 LINQ,我们可以使用非常简单的查询来做到这一点。
示例 4:获取产品名称等于 6 位数的元素
string myXML = "<Products><Product>Laptop</Product><Product>Printer</Product><Product>Mouse</Product></Products>";
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(myXML);
// Add one more Product at first position in xdoc
xdoc.Element("Products").AddFirst(new XElement("Product", "Mobile"));
var result = xdoc.Element("Products").Descendants().Where(s => s.Value.Length == 6);
输出:(result
内容)
<Product>Mobile</Product>
<Product>Laptop</Product>
还有很多其他方法可以帮助在层次结构中添加元素,删除元素。 我们还可以使用 xdoc.Save(filename)
方法保存 xml 文档。 希望此信息足以开始使用 XDocument
。 现在让我们看一下 XML 文档的两个主要部分,即 XElement
、XAttribute
。
使用动态 XML
本主题将介绍 XElement、XAttribute 和 XNode。 xml 的主要部分是元素和属性。
示例 5:创建 XElement 链的嵌套
XElement xElement = new XElement("Products",
new XElement("Product", "computer"),
new XElement("Product", "laptop"),
new XElement("Product", "mouse"),
new XElement("Product", "keyboard")
);
输出:(xElement
内容)
<Products>
<Product>computer</Product>
<Product>laptop</Product>
<Product>mouse</Product>
<Product>keyboard</Product>
</Products>
示例 6:使用 XElement 和 XAttribute 创建 XML
XElement xElement = new XElement("Products",
new XElement("Product", "computer", new XAttribute("ID", "1")),
new XElement("Product", "laptop", new XAttribute("ID", "2")),
new XElement("Product", "mouse", new XAttribute("ID", "3")),
new XElement("Product", "keyboard", new XAttribute("ID", "4"))
);
输出:(xElement
内容)
<Products>
<Product ID="1">computer</Product>
<Product ID="2">laptop</Product>
<Product ID="3">mouse</Product>
<Product ID="4">keyboard</Product>
</Products>
示例 7:从 XML 数据中删除元素
XElement xElement = new XElement("Products",
new XElement("Product", "computer", new XAttribute("ID", "1")),
new XElement("Product", "laptop", new XAttribute("ID", "2")),
new XElement("Product", "mouse", new XAttribute("ID", "3")),
new XElement("Product", "keyboard", new XAttribute("ID", "4"))
);
xElement.Descendants().Where(s => s.Attribute("ID").Value == "2").Remove();
输出:(xElement
内容)
<Products>
<Product ID="1">computer</Product>
<Product ID="3">mouse</Product>
<Product ID="4">keyboard</Product>
</Products>
阅读本文后,您不会成为专家,但至少您会发现它有助于您开始使用 LINQ 和 XML。 一旦您完全理解 LINQ 和 XML,您就可以使用此功能解决复杂的问题。
我发现这非常易于使用和维护。 希望您也喜欢这篇文章。 虽然我没有介绍 XElement、XAttribute、XNode 等的所有属性和方法... 但这些信息足以开始使用 XML。
下一步(参考书和链接)
C# 4.0 Nutshell(阅读第 10 章,必读)