LINQ TO XML 添加与判断

知识落伍了,记录下来

xml文件原形:

<items>
  <item kind="a">ba</item>
</items>
<items> <item kind="a">ba</item></items>

节点判断

Dim doc = XDocument.Load("post.xml")  
 Dim query = From p In doc.Descendants("item") _
Where p.Attribute(
"kind").Value = "a" And p.Value = "b"
If query.Count > 0 Then
存在()
End If

节点添加

Dim doc = XDocument.Load("post.xml")

doc.Element(
"items").Add(New XElement("item", "zz", New XAttribute("kind", "aa")))
doc.Save(
"post.xml")
为什么实例化时XElement时 item 可以转换成 System.Xml.Linq.XName 对象?这里不懂。
原文地址:https://www.cnblogs.com/zqonline/p/1635313.html