如何:修改 XML 文本 (Visual Basic)
更新:2007 年 11 月
Visual Basic 提供了修改 XML 文本的方便方式。您可以添加或删除元素和属性,还可以用新的 XML 元素替换现有元素。本主题提供了有关如何修改现有 XML 文本的多个示例。
修改 XML 文本的值
若要修改 XML 文本的值,需要获取对 XML 文本的引用,并将 Value 属性设置为期望值。
下面的代码示例更新了 XML 文档中所有 <Price> 元素的值。
For Each book In From element In catalog.<Catalog>.<Book> book.<Price>.Value = (book.<Price>.Value * 1.05).ToString("#.00") Next
下面显示了此代码示例中的示例源 XML 和修改后的 XML。
Source XML: <?xml version="1.0"?> <Catalog> <Book id="bk101"> <Author>Garghentini, Davide</Author> <Title>XML Developer's Guide</Title> <Price>44.95</Price> </Book> <Book id="bk331"> <Author>Spencer, Phil</Author> <Title>Developing Applications with Visual Basic .NET</Title> <Price>45.95</Price> </Book> </Catalog> Modified XML: <?xml version="1.0"?> <Catalog> <Book id="bk101"> <Author>Garghentini, Davide</Author> <Title>XML Developer's Guide</Title> <Price>47.20</Price> </Book> <Book id="bk331"> <Author>Spencer, Phil</Author> <Title>Developing Applications with Visual Basic .NET</Title> <Price>48.25</Price> </Book> </Catalog>
说明: Value 属性引用集合中的第一个 XML 元素。如果集合中有多个具有相同名称的元素,则设置 Value 属性仅影响集合中的第一个元素。
向 XML 文本添加属性
若要向 XML 文本添加属性,需要首先获取对 XML 文本的引用。然后可以通过添加新的 XML 属性 (Attribute) 轴属性 (Property) 来添加属性 (Attribute)。还可以使用 Add 方法向 XML 文本添加新的 XAttribute 对象。下面的示例演示了这两种方法。
Dim newAttribute = "editorEmail" Dim editorID = "someone@example.com" For Each book In From element In catalog.<Catalog>.<Book> ' Add an attribute by using an XML attribute axis property. book.@genre = "Computer" ' Add an attribute to the Attributes collection. book.Add(New XAttribute(newAttribute, editorID)) Next
下面显示了此代码示例中的示例源 XML 和修改后的 XML。
Source XML: <?xml version="1.0"?> <Catalog> <Book id="bk101" > <Author>Garghentini, Davide</Author> <Title>XML Developer's Guide</Title> <Price>44.95</Price> </Book> <Book id="bk331"> <Author>Spencer, Phil</Author> <Title>Developing Applications with Visual Basic .NET</Title> <Price>45.95</Price> </Book> </Catalog> Modified XML: <?xml version="1.0"?> <Catalog> <Book id="bk101" genre="Computer" editorEmail="someone@example.com"> <Author>Garghentini, Davide</Author> <Title>XML Developer's Guide</Title> <Price>44.95</Price> </Book> <Book id="bk331" genre="Computer" editorEmail="someone@example.com"> <Author>Spencer, Phil</Author> <Title>Developing Applications with Visual Basic .NET</Title> <Price>45.95</Price> </Book> </Catalog>
有关 XML 属性 (Attribute) 轴属性 (Property) 的更多信息,请参见 XML 属性 (Attribute) 轴属性 (Property)。
向 XML 文本添加元素
若要向 XML 文本添加元素,需要首先获取对 XML 文本的引用。然后可以使用 Add 方法添加一个新的 XElement 对象作为该元素的最后一个子元素。可以使用 AddFirst 方法添加一个新的 XElement 对象作为该元素的第一个子元素。
若要在相对于其他子元素的特定位置添加一个新元素,需要首先获取对某个相邻子元素的引用。然后可以使用 AddBeforeSelf 方法在该相邻子元素之前添加新的 XElement 对象。还可以使用 AddAfterSelf 方法在该相邻子元素之后添加新的 XElement 对象。
下面的示例演示了上述每种技术的示例。
Dim vbBook = From book In catalog.<Catalog>.<Book> _ Where book.<Title>.Value = _ "Developing Applications with Visual Basic .NET" vbBook(0).AddFirst(<Publisher>Microsoft Press</Publisher>) vbBook(0).Add(<PublishDate>2005-2-14</PublishDate>) vbBook(0).AddAfterSelf(<Book id="bk999"></Book>) vbBook(0).AddBeforeSelf(<Book id="bk000"></Book>)
下面显示了此代码示例中的示例源 XML 和修改后的 XML。
Source XML: <?xml version="1.0"?> <Catalog> <Book id="bk101" > <Author>Garghentini, Davide</Author> <Title>XML Developer's Guide</Title> <Price>44.95</Price> </Book> <Book id="bk331"> <Author>Spencer, Phil</Author> <Title>Developing Applications with Visual Basic .NET</Title> <Price>45.95</Price> </Book> </Catalog> Modified XML: <?xml version="1.0"?> <Catalog> <Book id="bk101" > <Author>Garghentini, Davide</Author> <Title>XML Developer's Guide</Title> <Price>44.95</Price> </Book> <Book id="bk000"></Book> <Book id="bk331"> <Publisher>Microsoft Press</Publisher> <Author>Spencer, Phil</Author> <Title>Developing Applications with Visual Basic .NET</Title> <Price>45.95</Price> <PublishDate>2005-2-14</PublishDate> </Book> <Book id="bk999"></Book> </Catalog>
从 XML 文本中移除元素或属性
若要从 XML 文本中移除元素或属性,需要获取对该元素或属性的引用,然后调用 Remove 方法,如下面的示例所示。
For Each book In From element In catalog.<Catalog>.<Book> book.Attributes("genre").Remove() Next For Each book In From element In catalog.<Catalog>.<Book> _ Where element.@id = "bk999" book.Remove() Next
下面显示了此代码示例中的示例源 XML 和修改后的 XML。
Source XML: <?xml version="1.0"?> <Catalog> <Book id="bk101" genre="Computer" editorEmail="someone@example.com"> <Author>Garghentini, Davide</Author> <Title>XML Developer's Guide</Title> <Price>44.95</Price> </Book> <Book id="bk000"></Book> <Book id="bk331" genre="Computer" editorEmail="someone@example.com"> <Author>Spencer, Phil</Author> <Title>Developing Applications with Visual Basic .NET</Title> <Price>45.95</Price> </Book> <Book id="bk999"></Book> </Catalog> Modified XML: <?xml version="1.0"?> <Catalog> <Book id="bk101" editorEmail="someone@example.com"> <Author>Garghentini, Davide</Author> <Title>XML Developer's Guide</Title> <Price>44.95</Price> </Book> <Book id="bk000"></Book> <Book id="bk331" editorEmail="someone@example.com"> <Author>Spencer, Phil</Author> <Title>Developing Applications with Visual Basic .NET</Title> <Price>45.95</Price> </Book></Catalog>
若要从 XML 文本中移除所有元素或属性,需要获取对该 XML 文本的引用,然后调用 RemoveAll 方法。
修改 XML 文本
若要更改 XML 元素的名称,需要首先获取对该元素的引用。然后,可以创建一个具有新名称的新的 XElement 对象,并将新的 XElement 对象传递给现有 XElement 对象的 ReplaceWith 方法。
如果要替换的元素具有必须保留的子元素,请将新的 XElement 对象的值设置为现有元素的 Nodes 属性。这会将新元素的值设置为现有元素的内部 XML。另外,还可以将新元素的值设置为现有元素的 Value 属性。
下面的代码示例将所有 <Description> 元素替换为 <Abstract> 元素。通过使用 <Description> XElement 对象的 Nodes 属性,将 <Description> 元素的内容保留在新的 <Abstract> 元素中。
For Each desc In From element In catalog.<Catalog>.<Book>.<Description> ' Replace and preserve inner XML. desc.ReplaceWith(<Abstract><%= desc.Nodes %></Abstract>) Next For Each price In From element In catalog.<Catalog>.<Book>.<Price> ' Replace with text value. price.ReplaceWith(<MSRP><%= price.Value %></MSRP>) Next
下面显示了此代码示例中的示例源 XML 和修改后的 XML。
Source XML: <?xml version="1.0"?> <Catalog> <Book id="bk101"> <Author>Garghentini, Davide</Author> <Title>XML Developer's Guide</Title> <Price>44.95</Price> <Description> An in-depth look at creating applications with <technology>XML</technology>. For <audience>beginners</audience> or <audience>advanced</audience> developers. </Description> </Book> <Book id="bk331"> <Author>Spencer, Phil</Author> <Title>Developing Applications with Visual Basic .NET</Title> <Price>45.95</Price> <Description> Get the expert insights, practical code samples, and best practices you need to advance your expertise with <technology>Visual Basic .NET</technology>. Learn how to create faster, more reliable applications based on professional, pragmatic guidance by today's top <audience>developers</audience>. </Description> </Book> </Catalog> Modified XML: <?xml version="1.0"?> <Catalog> <Book id="bk101"> <Author>Garghentini, Davide</Author> <Title>XML Developer's Guide</Title> <MSRP>44.95</MSRP> <Abstract> An in-depth look at creating applications with <technology>XML</technology>. For <audience>beginners</audience> or <audience>advanced</audience> developers. </Abstract> </Book> <Book id="bk331"> <Author>Spencer, Phil</Author> <Title>Developing Applications with Visual Basic .NET</Title> <MSRP>45.95</MSRP> <Abstract> Get the expert insights, practical code samples, and best practices you need to advance your expertise with <technology>Visual Basic .NET</technology>. Learn how to create faster, more reliable applications based on professional, pragmatic guidance by today's top <audience>developers</audience>. </Abstract> </Book> </Catalog>
请参见
任务
如何:从文件、字符串或流加载 XML (Visual Basic)