HOW TO:修改 XML 常值 (Visual Basic)
更新:2007 年 11 月
Visual Basic 為修改 XML 常值 (Literal) 提供了方便的方法。您可以加入或刪除項目和屬性 (Attribute),也可以用新的 XML 項目取代現有項目。本主題提供幾個修改現有 XML 常值的範例。
若要修改 XML 常值的值
若要修改 XML 常值的值,請先取得 XML 常值的參考,並將 Value 屬性 (Property) 設定為所要的值。
下列程式碼範例會更新 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 屬性 (Property) 會參考集合中的第一個 XML 項目。如果集合中有多個同名的項目,則設定 Value 屬性 (Property) 只會影響集合中的第一個項目。
若要將屬性加入至 XML 常值
若要將屬性 (Attribute) 加入至 XML 常值,請先取得 XML 常值的參考。接著您可以加入新的 XML 屬性 (Attribute) 軸屬性 (Property),即可加入屬性 (Attribute)。您也可以使用 Add 方法將新的 XAttribute 物件加入至 XML 常值。下列範例顯示這兩種選項。
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 屬性軸屬性。
若要將項目加入至 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 常值移除項目或屬性 (Attribute),請先取得該項目或屬性 (Attribute) 的參考,然後呼叫 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 常值的所有項目或屬性 (Attribute),請先取得 XML 常值的參考,然後呼叫 RemoveAll 方法。
若要修改 XML 常值
若要變更 XML 項目的名稱,請先取得該項目的參考。接著您可以建立具有新名稱的新 XElement 物件,並將新 XElement 物件傳遞至現有 XElement 物件的 ReplaceWith 方法。
如果要取代的項目具有需保留的子項目,請將新 XElement 物件的值設為現有項目的 Nodes 屬性 (Property)。如此會將新項目的值設為現有項目的內部 XML。否則,您可以將新項目的值設為現有項目的 Value 屬性 (Property)。
下列程式碼範例會將所有 <Description> 項目都取代為 <Abstract> 項目。其中會使用 <Description> XElement 物件的 Nodes 屬性 (Property),將 <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>
請參閱
工作
HOW TO:從檔案、字串或資料流載入 XML (Visual Basic)