Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Visual Basic proporciona formas cómodas de modificar literales XML. Puede agregar o eliminar elementos y atributos, y también puede reemplazar un elemento existente por un nuevo elemento XML. En este tema se proporcionan varios ejemplos de cómo modificar un literal XML existente.
Para modificar el valor de un literal de XML
Para modificar el valor de un literal XML, obtenga una referencia al literal XML y establezca la
Value
propiedad en el valor deseado.En el ejemplo de código siguiente se actualiza el valor de todos los <elementos Price> de un documento XML.
For Each book In From element In catalog.<Catalog>.<Book> book.<Price>.Value = (book.<Price>.Value * 1.05).ToString("#.00") Next
A continuación se muestra xml de origen de ejemplo y XML modificado de este ejemplo de código.
XML de origen:
<?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 modificado:
<?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:
La
Value
propiedad hace referencia al primer elemento XML de una colección. Si hay más de un elemento que tiene el mismo nombre en una colección, establecer laValue
propiedad afecta solo al primer elemento de la colección.
Para agregar un atributo a un literal XML
Para agregar un atributo a un literal XML, primero obtenga una referencia al literal XML. A continuación, puede agregar un atributo agregando una nueva propiedad de eje de atributos XML. También puede agregar un nuevo objeto XAttribute al literal XML con el método Add. En el ejemplo siguiente se muestran ambas opciones.
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
A continuación se muestra xml de origen de ejemplo y XML modificado de este ejemplo de código.
XML de origen:
<?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 modificado:
<?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>
Para obtener más información sobre las propiedades del eje de atributos XML, vea Propiedad del eje de atributos XML.
Para agregar un elemento a un literal XML
Para agregar un elemento a un literal XML, primero obtenga una referencia al literal XML. A continuación, puede agregar un nuevo objeto XElement como el último subelemento del elemento mediante el método Add. Puede agregar un objeto XElement nuevo como el primer subelemento usando el método AddFirst.
Para agregar un nuevo elemento en una ubicación específica relativa a otros sub-elementos, primero necesita obtener una referencia a un sub-elemento adyacente. A continuación, puede agregar el nuevo XElement objeto antes del sub-elemento adyacente mediante el AddBeforeSelf método . También puede agregar el nuevo XElement objeto después del sub-elemento adyacente mediante el AddAfterSelf método .
En el ejemplo siguiente se muestran ejemplos de cada una de estas técnicas.
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>)
A continuación se muestra xml de origen de ejemplo y XML modificado de este ejemplo de código.
XML de origen:
<?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 modificado:
<?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>
Para quitar un elemento o atributo de un literal XML
Para quitar un elemento o un atributo de un literal XML, obtenga una referencia al elemento o atributo y llame
Remove
al método , como se muestra en el ejemplo siguiente.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
A continuación se muestra xml de origen de ejemplo y XML modificado de este ejemplo de código.
XML de origen:
<?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 modificado:
<?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>
Para quitar todos los elementos o atributos de un literal XML, obtenga una referencia al literal XML y llame al RemoveAll método .
Para modificar un literal XML
Para cambiar el nombre de un elemento XML, primero obtenga una referencia al elemento . A continuación, puede crear un nuevo XElement objeto que tenga un nuevo nombre y pasar el nuevo XElement objeto al ReplaceWith método del objeto existente XElement .
Si el elemento que va a reemplazar tiene sub elementos que se deben conservar, establezca el valor del nuevo XElement objeto en la Nodes propiedad del elemento existente. Esto establecerá el valor del nuevo elemento en el XML interno del elemento existente. De lo contrario, puede establecer el valor del nuevo elemento en la
Value
propiedad del elemento existente.En el ejemplo de código siguiente se reemplazan todos los <elementos Description> por un <elemento Abstract> . El contenido del <elemento Description> se conserva en el nuevo <elemento Abstract> mediante la Nodes propiedad del <objeto 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
A continuación se muestra xml de origen de ejemplo y XML modificado de este ejemplo de código.
XML de origen:
<?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 modificado:
<?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>