Udostępnij za pośrednictwem


How to: Modify XML Literals (Visual Basic)

Visual Basic provides convenient ways to modify XML literals. You can add or delete elements and attributes, and you can also replace an existing element with a new XML element. This topic provides several examples of how to modify an existing XML literal.

To modify the value of an XML literal

  • To modify the value of an XML literal, obtain a reference to the XML literal and set the Value property to the desired value.

    The following code example updates the value of all the <Price> elements in an XML document.

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

    The following shows sample source XML and modified XML from this code example.

    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>
    

    Note

    The Value property refers to the first XML element in a collection. If there is more than one element that has the same name in a collection, setting the Value property affects only the first element in the collection.

To add an attribute to an XML literal

  • To add an attribute to an XML literal, first obtain a reference to the XML literal. You can then add an attribute by adding a new XML attribute axis property. You can also add a new XAttribute object to the XML literal by using the Add method. The following example shows both options.

    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
    

    The following shows sample source XML and modified XML from this code example.

    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>
    

    For more information about XML attribute axis properties, see XML Attribute Axis Property.

To add an element to an XML literal

  • To add an element to an XML literal, first obtain a reference to the XML literal. You can then add a new XElement object as the last sub-element of the element by using the Add method. You can add a new XElement object as the first sub-element by using the AddFirst method.

    To add a new element in a specific location relative to other sub-elements, first obtain a reference to an adjacent sub-element. You can then add the new XElement object before the adjacent sub-element by using the AddBeforeSelf method. You can also add the new XElement object after the adjacent sub-element by using the AddAfterSelf method.

    The following example shows examples of each of these techniques.

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

    The following shows sample source XML and modified XML from this code example.

    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>
    

To remove an element or attribute from an XML literal

  • To remove an element or an attribute from an XML literal, obtain a reference to the element or attribute and call the Remove method, as shown in the following example.

    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
    

    The following shows sample source XML and modified XML from this code example.

    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>
    

    To remove all elements or attributes from an XML literal, obtain a reference to the XML literal and call the RemoveAll method.

To modify an XML literal

  • To change the name of an XML element, first obtain a reference to the element. You can then create a new XElement object that has a new name and pass the new XElement object to the ReplaceWith method of the existing XElement object.

    If the element that you are replacing has sub-elements that must be preserved, set the value of the new XElement object to the Nodes property of the existing element. This will set the value of the new element to the inner XML of the existing element. Otherwise, you can set the value of the new element to the Value property of the existing element.

    The following code example replaces all <Description> elements with an <Abstract> element. The content of the <Description> element is preserved in the new <Abstract> element by using the Nodes property of the <Description> XElement object.

    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
    

    The following shows sample source XML and modified XML from this code example.

    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>
    

See Also

Tasks

How to: Load XML from a File, String, or Stream (Visual Basic)

Concepts

Introduction to LINQ in Visual Basic

Other Resources

Manipulating XML in Visual Basic

XML in Visual Basic

LINQ in Visual Basic