Working Around the XML Document C# Help

If you have been working with the XMLdocument, hamlet, XML, you will notice that it is quite large, Querying into the XMLdocument was shown in a couple of ways throughout this chapter, but this next section takes a look at reading and writing to the XMLdocument.

Reading From an XML Document

Earlier you saw just how easy it was to query into an XMLdocument using the LINQ query statements as shown here:
var query = from people in xdoc.Descendants(OPERSONAO)
select .,People. Value;
This query returned all the players that were found in the document. Using the ‘Element () method of the XDocumentobject, you can also get at specific values of the XMLdocument that you are working with. For instance, continuing to work with the hamlet, xml document, the following XMLfragment shows you how the title is represented in the XMLdocument:

<?xml version=Ol.00?>
<TITLE>TheTragedy of H~let, Prince of Denmark</TITLE>,
<!– XML removed for clarity –>
As you can see, the <TITLE>element is a nested element of the <PLAY>element. You can easily get at the
title by using the following bit of code in your console application:
XDocumentxdoc = XDocument.Load(eoC:\hamlet.xmlO);
Console.WriteLine(xdoc.Element(OPLAYO).Element(OTITLEO) .Value);
This bit of code will write out the title, The Tragedy; of Hamlet, Prince of Denmark, to the console
screen. In the code, you were able to work down the hierarchy of the XMLdocument by using two
Element () method calls – first calling the <PLAY>element and then the <TITLE>element found nested
within the <PLAY>element.

Looking more at the hamlet. xml document, you will see a large list of players that are defined with the use of the <PERSiONA>”element:
<TITLE>The Tragedy of Hamlet, Prince of Denmark</TITLE>
<!– XML removed for clarity –~ .:
<TITLE>Dramatis Persona</TITLE>
<PERSONA>CLAUDIUS, king of Denmark. </PERSONA>
<PERSONA>HAMLET, son to the late king,
and nephew to the present king. </PERSONA>”
<PERSONA> POLONIUS, lord chamberlain. </P~SONA>
<PERSONA>HORATIO, friend to Hamlet.</PERSONA>
<PERSONA>LAERTES, son to Polonius.</PERSONA~
<PERSONA>LUCIANUS, nephew to”the king.</PERSONA>
<!– XML removed for clarity –>
Using this XMLdocument, review the following bit of C* code’s use of this XML:.
XDocument x,document XDocument .Loa!1(i”C: \hamlet .xml”)
xdoc.Element(“PLAY”) .Element(“PERSONAE”).Element(“PERSONA”).Value);
This bit of code starts at <PLAY>, works down to the <PERSONAE> element, and then makes use of the
<PERSONA> element. How Using\is will produce the following results:
: CLAUDIUS, king of Demnark ”
” “. The reason for this is that, although there is a collection of <PERSONA> elements, you are dealing only
with the first one that is encountered using the Element () .Value call.

Writing to an XML “Document

In addition to reading &oman XMLdocument, you-can also write to the document just as easily. For instance, if you wanted to change ~e name of the first player of the Hamlet play file, you could make use of the following code to accomplish this task:
using System;
using System.Linq; –
using System.Xml.Linq; ”
name.pace ConsoleApplicationl
class Classl
static void Main()

XDocument xdoc = XDocument.Load(C:\hamlet.xml”);
xdoc.Element(“PLAY”) .Element (“PERSONA”) .
Element (“PERSONA”) .SetValue(“Bill Evjen, king of Denmark”);
Console.WriteLine(xdoc.Flement(“PLAY”) .
Element(“PERSONA”) .Element (“PERSONA”) .Value);
Console.ReadLine() ;

In this case, the firs~instance of the <PERSONA> element is overwritten with the value of Bi 11 Evjen, king of Denmark using the SetValue () method of the Element () object. After the SetValue () is caIled and the value is applied to the XMLdocument, the value is then retrieved using the same approach as before. When you run this bit of code, you can indeed see that the value of the first <PERSONA> element has been changed.

Another way to change the document (by adding items to it in this example) is to create the element you want as XElement objects and then add them to the document:

In this case, an XElement document is created called, The construction of  ill give you the following XMLoutput:
<PERSONA>Bill hjen. king of Denmark</PERSONA>
Then using the Element () .Add() method from the XDocument object, you are able to add the created element:
xdoc.Element(‘PLAY’) .Element (‘PERSONAE’) .Add(xel;
Now when you query all the players, you will find that instead of 26 as before, you now have 27 with the new one at the bottom of the list. In addition to Add (I,you can also use AddFirst (),which will do what it says – add it to the beginning of the list instead of the end as is the default.

Posted on October 31, 2015 in LINQ to XML

Share the Story

Back to Top
Share This