答案:VB.NET提供XmlDocument、XDocument、XmlReader和XmlWriter操作XML。1. XmlDocument基于DOM,适合读写修改整个文档;2. XDocument(LINQ to XML)语法简洁,支持LINQ查询,推荐使用;3. XmlReader以只进方式高效读取大文件;4. XmlWriter高性能生成XML。根据需求选择:灵活查询用XDocument,处理大文件用XmlReader/Writer,传统项目可用XmlDocument。

VB.NET 提供了多种方式来读取、创建、修改和保存 XML 文件,适合处理配置文件、数据交换等场景。本文将带你系统掌握 VB.NET 中操作 XML 的常用方法,涵盖 XmlDocument、XDocument(LINQ to XML)、XmlReader 和 XmlWriter 的使用。
1. 使用 XmlDocument 读写 XML
XmlDocument 是基于 DOM 的类,适合对整个 XML 文档进行操作,支持节点遍历和修改。
示例:创建并保存 XML 文件
Dim doc As New XmlDocument()Dim declaration As XmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
doc.AppendChild(declaration)
Dim root As XmlElement = doc.CreateElement("Books")
doc.AppendChild(root)
Dim book As XmlElement = doc.CreateElement("Book")
book.SetAttribute("ID", "1")
Dim title As XmlElement = doc.CreateElement("Title")
title.InnerText = "VB.NET 编程指南"
book.AppendChild(title)
root.AppendChild(book)
doc.Save("books.xml")
读取 XML 示例:
Dim doc As New XmlDocument()doc.Load("books.xml")
Dim books As XmlNodeList = doc.SelectNodes("//Book")
For Each book As XmlNode In books
Console.WriteLine("ID: " & book.Attributes("ID").Value)
Console.WriteLine("标题: " & book("Title").InnerText)
Next
2. 使用 LINQ to XML(推荐)
XDocument 是 .NET 3.5 引入的现代方式,语法简洁,支持 LINQ 查询,更易用。
创建 XML 文件:
Dim doc As New XDocument(New XElement("Books",
New XElement("Book",
New XAttribute("ID", "1"),
New XElement("Title", "VB.NET 高级编程"),
New XElement("Author", "张三")
)
)
)
doc.Save("books_linq.xml")
读取并查询 XML:
Dim doc As XDocument = XDocument.Load("books_linq.xml")Dim books = From b In doc.Descendants("Book")
Select New With {
.ID = b.Attribute("ID").Value,
.Title = b.Element("Title").Value,
.Author = b.Element("Author").Value
}
For Each book In books
Console.WriteLine($"ID: {book.ID}, 书名: {book.Title}, 作者: {book.Author}")
Next
修改 XML 内容:
Dim doc As XDocument = XDocument.Load("books_linq.xml")Dim book = doc.Descendants("Book")
.Where(Function(x) x.Attribute("ID").Value = "1")
.FirstOrDefault()
If book IsNot Nothing Then
book.Element("Title").Value = "更新后的书名"
doc.Save("books_linq.xml")
End If
3. 使用 XmlReader 高效读取大文件
XmlReader 以只进只读方式解析 XML,内存占用小,适合处理大型 XML 文件。
Using reader As XmlReader = XmlReader.Create("books.xml")While reader.Read()
If reader.IsStartElement() Then
Select Case reader.Name
Case "Title"
reader.Read()
Console.WriteLine("书名: " & reader.Value)
Case "Author"
reader.Read()
Console.WriteLine("作者: " & reader.Value)
End Select
End If
End While
End Using
4. 使用 XmlWriter 高效写入 XML
XmlWriter 用于快速生成 XML,避免构建完整对象模型,性能高。
Using writer As XmlWriter = XmlWriter.Create("output.xml")writer.WriteStartDocument()
writer.WriteStartElement("Users")
writer.WriteStartElement("User")
writer.WriteAttributeString("ID", "1")
writer.WriteElementString("Name", "李四")
writer.WriteElementString("Email", "lisi@example.com")
writer.WriteEndElement() ' User
writer.WriteEndElement() ' Users
writer.WriteEndDocument()
End Using
基本上就这些。根据需求选择合适的方法:需要灵活查询用 XDocument,处理大文件用 XmlReader/Writer,传统项目可用 XmlDocument。掌握这些,就能轻松应对大多数 XML 操作场景。










