Bagikan melalui


Panduan: Mengubah Literal XML (Visual Basic)

Visual Basic menyediakan cara mudah untuk memodifikasi literal XML. Anda dapat menambahkan atau menghapus elemen dan atribut, dan Anda juga dapat mengganti elemen yang ada dengan elemen XML baru. Topik ini menyediakan beberapa contoh cara memodifikasi literal XML yang ada.

Untuk mengubah nilai literal XML

  1. Untuk mengubah nilai XML literal, dapatkan referensi ke XML literal dan atur Value properti ke nilai yang diinginkan.

    Contoh kode berikut memperbarui nilai semua <Price> elemen dalam dokumen XML.

    For Each book In From element In catalog.<Catalog>.<Book>
      book.<Price>.Value = (book.<Price>.Value * 1.05).ToString("#.00")
    Next
    

    Berikut ini memperlihatkan sampel XML sumber dan XML yang dimodifikasi dari contoh kode ini.

    Sumber 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>
    

    XML yang dimodifikasi:

    <?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>
    

    Nota

    Properti Value mengacu pada elemen XML pertama dalam koleksi. Jika ada lebih dari satu elemen yang memiliki nama yang sama dalam koleksi, pengaturan Value properti hanya memengaruhi elemen pertama dalam koleksi.

Untuk menambahkan atribut ke XML literal

  1. Untuk menambahkan atribut ke xml literal, pertama-tama dapatkan referensi ke literal XML. Anda kemudian dapat menambahkan atribut dengan menambahkan properti sumbu atribut XML baru. Anda juga dapat menambahkan objek baru XAttribute ke XML literal dengan menggunakan Add metode . Contoh berikut menunjukkan kedua opsi.

    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
    

    Berikut ini memperlihatkan sampel XML sumber dan XML yang dimodifikasi dari contoh kode ini.

    Sumber 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>
    

    XML yang dimodifikasi:

    <?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>
    

    Untuk informasi selengkapnya tentang properti sumbu atribut XML, lihat Properti Sumbu Atribut XML.

Untuk menambahkan elemen ke literal XML

  1. Untuk menambahkan elemen ke literal XML, pertama-tama dapatkan referensi ke literal XML. Anda kemudian dapat menambahkan objek baru XElement sebagai sub-elemen terakhir dari elemen dengan menggunakan Add metode . Anda dapat menambahkan objek baru XElement sebagai sub-elemen pertama dengan menggunakan AddFirst metode .

    Untuk menambahkan elemen baru di lokasi tertentu yang relatif terhadap sub-elemen lain, pertama-tama dapatkan referensi ke sub-elemen yang berdekatan. Anda kemudian dapat menambahkan objek baru XElement sebelum sub-elemen yang berdekatan AddBeforeSelf dengan menggunakan metode . Anda juga dapat menambahkan objek baru XElement setelah sub-elemen yang berdekatan AddAfterSelf dengan menggunakan metode .

    Contoh berikut menunjukkan contoh masing-masing teknik ini.

    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>)
    

    Berikut ini memperlihatkan sampel XML sumber dan XML yang dimodifikasi dari contoh kode ini.

    Sumber 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>
    

    XML yang dimodifikasi:

    <?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>
    

Untuk menghapus elemen atau atribut dari XML literal

  1. Untuk menghapus elemen atau atribut dari xml literal, dapatkan referensi ke elemen atau atribut dan panggil Remove metode , seperti yang ditunjukkan dalam contoh berikut.

    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
    

    Berikut ini memperlihatkan sampel XML sumber dan XML yang dimodifikasi dari contoh kode ini.

    Sumber 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>
    

    XML yang dimodifikasi:

    <?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>
    

    Untuk menghapus semua elemen atau atribut dari XML literal, dapatkan referensi ke XML literal dan panggil metode RemoveAll.

Untuk mengubah literal XML

  1. Untuk mengubah nama elemen XML, pertama-tama dapatkan referensi ke elemen . Anda kemudian dapat membuat objek baru XElement yang memiliki nama baru dan meneruskan objek baru XElement ke ReplaceWith metode objek yang ada XElement .

    Jika elemen yang Anda ganti memiliki sub-elemen yang harus dipertahankan, atur nilai objek baru XElement ke Nodes properti elemen yang ada. Ini akan mengatur nilai elemen baru ke XML dalam dari elemen yang ada. Jika tidak, Anda dapat mengatur nilai elemen baru ke Value properti elemen yang ada.

    Contoh kode berikut mengganti semua <Description> elemen dengan <Abstract> elemen . Konten elemen <Description> dipertahankan dalam elemen baru <Abstract> dengan menggunakan properti Nodes dari objek <Description>XElement.

    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
    

    Berikut ini memperlihatkan sampel XML sumber dan XML yang dimodifikasi dari contoh kode ini.

    Sumber 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>
    

    XML yang dimodifikasi:

    <?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>
    

Lihat juga