Tải bản đầy đủ - 0 (trang)
XElement.SetElementValue() on Child XElement Objects

XElement.SetElementValue() on Child XElement Objects

Tải bản đầy đủ - 0trang

C H A P T E R 7 N T H E LI N Q T O X M L A P I



307



XNode.AddAfterSelf()

After using the VB.NET XML features to get a reference to the second >kkgL]npe_el]jp element in the previous example, the example in Listing 7-66 is sure to be anticlimactic. I will

just get a reference to the first >kkgL]npe_el]jp element using the normal LINQ to XML API’s

Ahaiajp and Ahaiajpo methods, and add the new >kkgL]npe_el]jp element after it using the

=``=bpanOahb method.

Listing 7-66. Adding a Node in a Specific Location of the Specified Node’s Child Nodes with

AddAfterSelf

@eit@k_qiajp9[

8;tihranoekj9-*,;:

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

t@k_qiajp*8>kkgL]npe_el]jpo:$,%*[

=``$8>kkgL]npe_el]jppula9A`epkn:

8BenopJ]ia:As]j8+BenopJ]ia:

8H]opJ]ia:>q_gejcd]i8+H]opJ]ia:

8+>kkgL]npe_el]jp:%

t@k_qiajp*Ahaiajp$>kkgL]npe_el]jpo%*Ahaiajpo$>kkgL]npe_el]jp%*Benop$%*[

=``=bpanOahb$8>kkgL]npe_el]jppula9Pa_dje_]hA`epkn:

8BenopJ]ia:Fka8+BenopJ]ia:

8H]opJ]ia:N]ppv8+H]opJ]ia:

8+>kkgL]npe_el]jp:%

?kjokha*SnepaHeja$t@k_qiajp%

This example just seems trivial after the previous one:

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8>kkgL]npe_el]jppula9Pa_dje_]hA`epkn:

8BenopJ]ia:Fka8+BenopJ]ia:

8H]opJ]ia:N]ppv8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8>kkgL]npe_el]jppula9A`epkn:

8BenopJ]ia:As]j8+BenopJ]ia:

8H]opJ]ia:>q_gejcd]i8+H]opJ]ia:



www.it-ebooks.info

16445ch07.indd 307



7/17/09 2:50:39 PM



308



CH APT ER 7 N THE L INQ TO X ML A P I



8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:



Deleting Nodes

Deleting nodes is accomplished with either of two methods: Naikra or Naikra=hh.

In addition to reading about the following ways to delete nodes, be sure to check out the

section “XElement.SetElementValue() on Child XElement Objects” later in this chapter.



XNode.Remove()

The Naikra method removes any node, as well as its child nodes and attributes, from an XML

tree. In the first example, I construct an XML tree and obtain a reference to the first book participant element as I did in some of the previous examples. I display the XML tree after the

construction but before deleting any nodes. I then delete the first >kkgL]npe_el]jp element

and display the resulting XML tree, as shown in Listing 7-67.

Listing 7-67. Deleting a Specific Node with Remove

@eit@k_qiajp9[

8;tihranoekj9-*,aj_k`ejc9qpb)4op]j`]hkja9uao;:

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8>kkgL]npe_el]jppula9A`epkn:

8BenopJ]ia:As]j8+BenopJ]ia:

8H]opJ]ia:>q_gejcd]i8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

@eibenopL]npe_el]jp9t@k_qiajp***8>kkgL]npe_el]jp:$,%

?kjokha*SnepaHeja$Ouopai*Ajrenkjiajp*JasHeja'>abknajk`a`ahapekj%

?kjokha*SnepaHeja$t@k_qiajp%

benopL]npe_el]jp*Naikra$%

?kjokha*SnepaHeja$Ouopai*Ajrenkjiajp*JasHeja'=bpanjk`a`ahapekj%

?kjokha*SnepaHeja$t@k_qiajp%

If all goes as planned, I should get the XML tree initially with the first >kkgL]npe_el]jp

element and subsequently without it:

>abknajk`a`ahapekj

8>kkgL]npe_el]jpo:



www.it-ebooks.info

16445ch07.indd 308



7/17/09 2:50:39 PM



C H A P T E R 7 N T H E LI N Q T O X M L A P I



309



8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8>kkgL]npe_el]jppula9A`epkn:

8BenopJ]ia:As]j8+BenopJ]ia:

8H]opJ]ia:>q_gejcd]i8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

=bpanjk`a`ahapekj

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9A`epkn:

8BenopJ]ia:As]j8+BenopJ]ia:

8H]opJ]ia:>q_gejcd]i8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

As you can see, the first >kkgL]npe_el]jp element is gone after the node deletion.



IEnumerable(Of T).Remove()

In the previous case, I call the Naikra method on a single TJk`a object. I can also call Naikra

on a sequence (EAjqian]^ha$KbP%). Listing 7-68 is an example where I use the @ao_aj`]jpo

method of the document to recursively traverse all the way down the XML tree, returning only

those elements whose name is BenopJ]ia by using the Sdana operator. I then call the Naikra

method on the resulting sequence.

Listing 7-68. Deleting a Sequence of Nodes with Remove

@eit@k_qiajp9[

8;tihranoekj9-*,aj_k`ejc9qpb)4op]j`]hkja9uao;:

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8>kkgL]npe_el]jppula9A`epkn:

8BenopJ]ia:As]j8+BenopJ]ia:

8H]opJ]ia:>q_gejcd]i8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

t@k_qiajp*@ao_aj`]jpo$%*Sdana$Bqj_pekj$a%a*J]ia9BenopJ]ia%*Naikra$%

?kjokha*SnepaHeja$t@k_qiajp%



www.it-ebooks.info

16445ch07.indd 309



7/17/09 2:50:39 PM



310



CH APT ER 7 N THE L INQ TO X ML A P I



I like this example because I really start to tie all the elements of LINQ together with it. I

am using the T@k_qiajp*@ao_aj`]jpo method to get all the child nodes returned in a sequence,

and then I call the Sdana Standard Query Operator to filter just the ones matching the search

criteria, which in this case are elements named BenopJ]ia. This returns a sequence that I then

call the Naikra method on. Sweet! Here are the results:

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8>kkgL]npe_el]jppula9A`epkn:

8H]opJ]ia:>q_gejcd]i8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

Notice that I no longer have any BenopJ]ia elements.



NTip The purpose of this chapter is to examine the LINQ to XML API, and in Listing 7-68, I demonstrated how to remove elements matching a specified name with that API. We’ll keep it our secret that,

by using the VB.NET 2008 axis properties, we can simply replace the line removing the elements with

t@k_qiajp***8BenopJ]ia:*Naikra$%.



XElement.RemoveAll()

Sometimes, you may want to delete the content of an element but not the element itself. This

is what the Naikra=hh method is for. Listing 7-69 is an example.

Listing 7-69. Removing a Node’s Content with RemoveAll

@eit@k_qiajp9[

8;tihranoekj9-*,aj_k`ejc9qpb)4op]j`]hkja9uao;:

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8>kkgL]npe_el]jppula9A`epkn:

8BenopJ]ia:As]j8+BenopJ]ia:

8H]opJ]ia:>q_gejcd]i8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

?kjokha*SnepaHeja$Ouopai*Ajrenkjiajp*JasHeja'>abknanaikrejcpda_kjpajp*%

?kjokha*SnepaHeja$t@k_qiajp%



www.it-ebooks.info

16445ch07.indd 310



7/17/09 2:50:39 PM



C H A P T E R 7 N T H E LI N Q T O X M L A P I



311



t@k_qiajp*Ahaiajp$>kkgL]npe_el]jpo%*Naikra=hh$%

?kjokha*SnepaHeja$Ouopai*Ajrenkjiajp*JasHeja'=bpannaikrejcpda_kjpajp*%

?kjokha*SnepaHeja$t@k_qiajp%

Here I display the document first before removing the content of the >kkgL]npe_el]jpo

node. Then, I remove the content of the >kkgL]npe_el]jpo node and display the document

again. Since you could be from Missouri, I had better show you the results:

>abknanaikrejcpda_kjpajp*

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8>kkgL]npe_el]jppula9A`epkn:

8BenopJ]ia:As]j8+BenopJ]ia:

8H]opJ]ia:>q_gejcd]i8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

=bpannaikrejcpda_kjpajp*

8>kkgL]npe_el]jpo+:



Updating Nodes

Several of the subclasses of TJk`a, such as TAhaiajp, TPatp, and T?kiiajp, have a R]hqa property that can be directly updated. Others, such as T@k_qiajpPula and TLnk_aooejcEjopnq_pekj,

have specific properties that each can be updated. For an element, in addition to modifying the R]hqa property, you can change its value by calling the TAhaiajp*OapAhaiajpR]hqa or

T?kjp]ejan*Nalh]_a=hh methods covered later in this chapter.



XElement.Value on XElement Objects, XText.Value on XText Objects, and XComment.Value

on XComment Objects

Each of these subclasses of TJk`a has a R]hqa property that can be set to update the node’s

value. Listing 7-70 demonstrates all of them.

Listing 7-70. Updating a Node’s Value

@eit@k_qiajp9[

8;tihranoekj9-*,aj_k`ejc9qpb)4op]j`]hkja9uao;:

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8))Pdeoeo]jas]qpdkn*)):

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:



www.it-ebooks.info

16445ch07.indd 311



7/17/09 2:50:39 PM



312



CH APT ER 7 N THE L INQ TO X ML A P I



8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

@eibenopL]npe_el]jp9t@k_qiajp***8>kkgL]npe_el]jp:$,%

?kjokha*SnepaHeja$>abknaql`]pejcjk`ao6%

?kjokha*SnepaHeja$t@k_qiajp%

#Jks(E#hhql`]pa]jahaiajp(]_kiiajp(]j`]patpjk`a*

benopL]npe_el]jp*Ahaiajp$BenopJ]ia%*R]hqa9Ole`an

benopL]npe_el]jp*Jk`ao$%*KbPula$KbT?kiiajp%$%*Oejcha$%*R]hqa9[

=qpdknkbLnkHEJM6H]jcq]caEjpacn]pa`MqanuejR>*Jap.,,4*

?Pula$benopL]npe_el]jp*Ahaiajp$BenopJ]ia%*JatpJk`a(TAhaiajp%[

*Jk`ao$%*KbPula$KbTPatp%$%*Oejcha$%*R]hqa9D]uao(Fn*

?kjokha*SnepaHeja$=bpanql`]pejcjk`ao6%

?kjokha*SnepaHeja$t@k_qiajp%

In this example, I update the BenopJ]ia element first, using its R]hqa property, followed by

the comment using its R]hqa property, finally followed by updating the H]opJ]ia element by

accessing its value through its child TPatp object’s R]hqa property. Notice the flexibility LINQ

to XML provides for getting references to the different objects I want to update. Just remember

that it isn’t necessary for me to access the H]opJ]ia element’s value by getting the TPatp object

from its child nodes. I did that merely for demonstration purposes. Other than that, I would

have directly accessed its R]hqa property. Here are the results:

>abknaql`]pejcjk`ao6

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8))Pdeoeo]jas]qpdkn*)):

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

=bpanql`]pejcjk`ao6

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8))=qpdknkbLnkHEJM6H]jcq]caEjpacn]pa`MqanuejR>*JAP.,,4*)):

8BenopJ]ia:Ole`an8+BenopJ]ia:

8H]opJ]ia:D]uao(Fn*8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

As you can see, all of the node’s values are updated.



www.it-ebooks.info

16445ch07.indd 312



7/17/09 2:50:39 PM



C H A P T E R 7 N T H E LI N Q T O X M L A P I



313



XDocumentType.Name, XDocumentType.PublicId, XDocumentType.SystemId, and

XDocumentType.InternalSubset on XDocumentType Objects

To update a document type node, the T@k_qiajpPula class provides four properties for

updating its values. Listing 7-71 is some sample code demonstrating this.

Listing 7-71. Updating the Document Type

#Esehhqoapdeopkopkna]nabanaj_apkpda@k_qiajpPulabknh]pan]__aoo*

@ei`k_Pula=oT@k_qiajpPula9[

JasT@k_qiajpPula$>kkgL]npe_el]jpo(Jkpdejc(>kkgL]npe_el]jpo*`p`(Jkpdejc%

@eit@k_qiajp=oJasT@k_qiajp$`k_Pula(JasTAhaiajp$>kkgL]npe_el]jpo%%

?kjokha*SnepaHeja$>abknaql`]pejc`k_qiajppula6%

?kjokha*SnepaHeja$t@k_qiajp%

`k_Pula*J]ia9Iu>kkgL]npe_el]jpo

`k_Pula*OuopaiE`9dppl6++sss*okiasdana*_ki+@P@o+Iu>kkgL]npe_el]jpo*@P@

`k_Pula*Lq^he_E`9)++@P@o++PATP>kkgL]npe_el]jpo++AJ

?kjokha*SnepaHeja$=bpanql`]pejc`k_qiajppula6%

?kjokha*SnepaHeja$t@k_qiajp%

Here are the results of this code:

>abknaql`]pejc`k_qiajppula6

8@K?PULA>kkgL]npe_el]jpoOUOPAI>kkgL]npe_el]jpo*`p`:

8>kkgL]npe_el]jpo+:

=bpanql`]pejc`k_qiajppula6

8@K?PULAIu>kkgL]npe_el]jpoLQ>HE?)++@P@o++PATP>kkgL]npe_el]jpo++AJ

dppl6++sss*okiasdana*_ki+@P@o+Iu>kkgL]npe_el]jpo*@P@:

8>kkgL]npe_el]jpo+:



XProcessingInstruction.Target on XProcessingInstruction Objects and

XProcessingInstruction.Data on XProcessingInstruction Objects

To update the value of a processing instruction, simply modify the P]ncap and @]p] properties

of the TLnk_aooejcEjopnq_pekj object. Listing 7-72 is an example.

Listing 7-72. Updating a Processing Instruction

#Esehhqoapdeopkopkna]nabanaj_abknh]pan]__aoo*

@eilnk_Ejop=oTLnk_aooejcEjopnq_pekj9[

JasTLnk_aooejcEjopnq_pekj$>kkg?]p]hkcan(kqp)kb)lnejp%

@eit@k_qiajp=oJasT@k_qiajp$JasTAhaiajp$>kkgL]npe_el]jpo%(lnk_Ejop%



www.it-ebooks.info

16445ch07.indd 313



7/17/09 2:50:39 PM



314



CH APT ER 7 N THE L INQ TO X ML A P I



?kjokha*SnepaHeja$>abknaql`]pejclnk_aooejcejopnq_pekj6%

?kjokha*SnepaHeja$t@k_qiajp%

lnk_Ejop*P]ncap9>kkgL]npe_el]jp?kjp]_pI]j]can

lnk_Ejop*@]p]9ql`]pa

?kjokha*SnepaHeja$=bpanql`]pejclnk_aooejcejopnq_pekj6%

?kjokha*SnepaHeja$t@k_qiajp%

Now let’s take a look at the output:

>abknaql`]pejclnk_aooejcejopnq_pekj6

8>kkgL]npe_el]jpo+:

8;>kkg?]p]hkcankqp)kb)lnejp;:

=bpanql`]pejclnk_aooejcejopnq_pekj6

8>kkgL]npe_el]jpo+:

8;>kkgL]npe_el]jp?kjp]_pI]j]canql`]pa;:



XElement.ReplaceAll()

The Nalh]_a=hh method is useful for replacing an element’s entire subtree of XML. You can

pass a simple value, such as a new string or a numeric type; or because there is an overloaded

method that accepts multiple objects via the L]n]i=nn]u keyword, an entire subtree can be

changed. The Nalh]_a=hh method also replaces attributes. Listing 7-73 is some sample code.

Listing 7-73. Using ReplaceAll to Change an Element’s Subtree

@eit@k_qiajp9[

8;tihranoekj9-*,aj_k`ejc9qpb)4op]j`]hkja9uao;:

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

@eibenopL]npe_el]jp9t@k_qiajp***8>kkgL]npe_el]jp:$,%

?kjokha*SnepaHeja$Ouopai*Ajrenkjiajp*JasHeja">abknaql`]pejcahaiajpo6%

?kjokha*SnepaHeja$t@k_qiajp%

benopL]npe_el]jp*Nalh]_a=hh$[

8BenopJ]ia:As]j8+BenopJ]ia:([

8H]opJ]ia:>q_gejcD]i8+H]opJ]ia:%

?kjokha*SnepaHeja$Ouopai*Ajrenkjiajp*JasHeja"=bpanql`]pejcahaiajpo6%

?kjokha*SnepaHeja$t@k_qiajp%



www.it-ebooks.info

16445ch07.indd 314



7/17/09 2:50:39 PM



C H A P T E R 7 N T H E LI N Q T O X M L A P I



315



Notice that when I replaced the content with the Nalh]_a=hh method, I omitted specifying an attribute. Also, please notice that I used the overload that accepts multiple objects and

passed it two XML literals. As you would expect, the content is replaced:

>abknaql`]pejcahaiajpo6

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

=bpanql`]pejcahaiajpo6

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jp:

8BenopJ]ia:As]j8+BenopJ]ia:

8H]opJ]ia:>q_gejcd]i8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

Notice that the >kkgL]npe_el]jp pula attribute is now gone. This is interesting in that attributes are not child nodes of an element. But the Nalh]_a=hh method replaces them as well.



XElement.SetElementValue() on Child XElement Objects

Don’t let this simply named method fool you; it’s a powerhouse. It has the ability to add,

change, and remove elements. Furthermore, it performs these operations on the child elements of the element you call it on. Stated differently, you call the OapAhaiajpR]hqa method

on a parent element to affect its content, meaning its child elements.

When calling the OapAhaiajpR]hqa method, you pass it the name of the child element you

want to set and the value you want to set it to. If a child element is found by that name, its

value is updated, as long as the passed value is not Jkpdejc. If the passed value is Jkpdejc, that

found child element will be removed. If an element by that name is not found, it will be added

with the passed value. Wow, what a method!

Also, the OapAhaiajpR]hqa method will only affect the first child element it finds with the

specified name. Any subsequent elements with the same name will not be affected, either by

the value being changed to the one passed in or the element being removed, because that

passed value is Jkpdejc.

Listing 7-74 is an example demonstrating all uses: update, add, and delete.

Listing 7-74. Using SetElementValue to Update, Add, and Delete Child Elements

@eit@k_qiajp9[

8;tihranoekj9-*,aj_k`ejc9qpb)4op]j`]hkja9uao;:

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:



www.it-ebooks.info

16445ch07.indd 315



7/17/09 2:50:39 PM



316



CH APT ER 7 N THE L INQ TO X ML A P I



8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

@eibenopL]npe_el]jp9t@k_qiajp***8>kkgL]npe_el]jp:$,%

?kjokha*SnepaHeja$Ouopai*Ajrenkjiajp*JasHeja">abknaql`]pejcahaiajpo6%

?kjokha*SnepaHeja$t@k_qiajp%

#Benop(EsehhqoaTAhaiajp*OapAhaiajpR]hqapkql`]papdar]hqakb]jahaiajp*

#Oej_a]jahaiajpj]ia`BenopJ]iaeopdana(epor]hqasehh^aql`]pa`pk@ajju*

benopL]npe_el]jp*OapAhaiajpR]hqa$BenopJ]ia(@ajju%

#Oa_kj`(EsehhqoaTAhaiajp*OapAhaiajpR]hqapk]``]jahaiajp*

#Oej_ajkahaiajpj]ia`Ie``haEjepe]hateopo(kjasehh^a]``a`*

benopL]npe_el]jp*OapAhaiajpR]hqa$Ie``haEjepe]h(A%

#Pden`(EsehhqoaTAhaiajp*OapAhaiajpR]hqapknaikra]jahaiajp*

#Oappejc]jahaiajp#or]hqapkJkpdejcsehhnaikraep*

benopL]npe_el]jp*OapAhaiajpR]hqa$H]opJ]ia(Jkpdejc%

?kjokha*SnepaHeja$Ouopai*Ajrenkjiajp*JasHeja"=bpanql`]pejcahaiajpo6%

?kjokha*SnepaHeja$t@k_qiajp%

As you can see, first I call the OapAhaiajpR]hqa method on the benopL]npe_el]jp element’s

child element named BenopJ]ia. Since an element already exists by that name, its value will be

updated. Next, I call the OapAhaiajpR]hqa method on the benopL]npe_el]jp element’s child element named Ie``haEjepe]h. Since no element exists by that name, the element will be added.

Lastly, I call the OapAhaiajpR]hqa method on the benopL]npe_el]jp element’s child element

named H]opJ]ia and pass a Jkpdejc. Since a Jkpdejc is passed, the H]opJ]ia element will be

removed. Look at the flexibility that the OapAhaiajpR]hqa method provides. I know you can’t

wait to see the results:

>abknaql`]pejcahaiajpo6

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

=bpanql`]pejcahaiajpo6

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdkn:

8BenopJ]ia:@ajju8+BenopJ]ia:

8Ie``haEjepe]h:A8+Ie``haEjepe]h:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:



www.it-ebooks.info

16445ch07.indd 316



7/17/09 2:50:39 PM



C H A P T E R 7 N T H E LI N Q T O X M L A P I



317



How cool is that? The BenopJ]ia element’s value was updated, the Ie``haEjepe]h element

was added, and the H]opJ]ia element was removed.



NCaution Just because calling the OapAhaiajpR]hqa method with a value of Jkpdejc removes the

node, don’t make the mistake of thinking that manually setting an element’s value to Jkpdejc is the same

as removing it in the LINQ to XML API. This is merely the behavior of the OapAhaiajpR]hqa method. If you

attempt to set an element’s value to Jkpdejc using its R]hqa property, an exception will be thrown.



XML Attributes

As I previously mentioned, with the LINQ to XML API, attributes are implemented with the

T=ppne^qpa class, and unlike the W3C DOM XML API, they do not inherit from a node. Therefore, they have no inheritance relationship with elements. However, in the LINQ to XML API,

they are every bit as easy to work with as elements. Let’s take a look.



Attribute Creation

Attributes are created just like elements and most other LINQ to XML classes. This topic is

covered in the “Creating Attributes with XAttribute” section previously in this chapter.



Attribute Traversal

Attributes can be traversed using the TAhaiajp*Benop=ppne^qpa, TAhaiajp*H]op=ppne^qpa,

T=ppne^qpa*Jatp=ppne^qpa, and T=ppne^qpa*Lnarekqo=ppne^qpa properties and the TAhaiajp*

=ppne^qpa and TAhaiajp*=ppne^qpao methods. These are described in the following sections.



Forward with XElement.FirstAttribute

You can gain access to an element’s attributes by accessing its first attribute using the element’s Benop=ppne^qpa property. Listing 7-75 is an example.

Listing 7-75. Accessing an Element’s First Attribute with the FirstAttribute Property

@eit@k_qiajp9[

8;tihranoekj9-*,aj_k`ejc9qpb)4op]j`]hkja9uao;:

8>kkgL]npe_el]jpo:

8>kkgL]npe_el]jppula9=qpdknatlaneaj_a9benop)peiah]jcq]ca9Ajcheod:

8BenopJ]ia:@ajjeo8+BenopJ]ia:

8H]opJ]ia:D]uao8+H]opJ]ia:

8+>kkgL]npe_el]jp:

8+>kkgL]npe_el]jpo:

@eibenopL]npe_el]jp9t@k_qiajp***8>kkgL]npe_el]jp:$,%

?kjokha*SnepaHeja$benopL]npe_el]jp*Benop=ppne^qpa%



www.it-ebooks.info

16445ch07.indd 317



7/17/09 2:50:39 PM



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

XElement.SetElementValue() on Child XElement Objects

Tải bản đầy đủ ngay(0 tr)

×