15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

XMl Data Type ❘ 975<br />

connection.Open();<br />

var reader = comm<strong>and</strong>.ExecuteReader(Comm<strong>and</strong>Behavior.CloseConnection);<br />

while (reader.Read())<br />

{<br />

SqlXml xml = reader.GetSqlXml(2);<br />

var doc = new XmlDocument();<br />

doc.LoadXml(xml.Value);<br />

XmlNode examNode = doc.SelectSingleNode("//Exam");<br />

Console.WriteLine("Exam: {0}", examNode.Attributes["Number"].Value);<br />

XmlNode titleNode = examNode.SelectSingleNode("./Title");<br />

Console.WriteLine("Title: {0}", titleNode.InnerText);<br />

Console.Write("Course(s): ");<br />

foreach (XmlNode courseNode in examNode.SelectNodes("./Course"))<br />

{<br />

Console.Write("{0} ", courseNode.InnerText);<br />

}<br />

Console.WriteLine();<br />

}<br />

reader.Close();<br />

c o d e s n i p p e t X m l S a m p l e s / P r o g ra m . c s<br />

T he XmlReader <strong>and</strong> XmlDocument classes are discussed in Chapter 33, “Manipulating<br />

XML.”<br />

With .<strong>NET</strong> 4 there’s another option to access the XML column<br />

from the database. You can combine LINQ to SQL <strong>and</strong> LINQ to<br />

XML, which makes the programming code smaller.<br />

You can use the ADO.<strong>NET</strong> Entity Framework designer by<br />

selecting the ADO.<strong>NET</strong> Entity Data Model template from the<br />

Data templates category. Name the fi le ExamsModel.edmx to<br />

create a mapping for the database SqlServerSampleDB. Select the<br />

Exams table <strong>and</strong> select the option to singularize object names.<br />

After you create the model with the wizard, a design surface, as<br />

shown in Figure 34 - 4, opens.<br />

The data context class that is created by the designer has the<br />

name ExamsEntities <strong>and</strong> defi nes a property Exams to return all<br />

exam rows. Here a foreach statement is used to iterate through<br />

all records. Of course you can also defi ne a LINQ query with<br />

a where expression if not all records are required. The Exam<br />

figure 34-4<br />

class defi nes the properties Id , Number , <strong>and</strong> Info according to<br />

the columns in the database table. The Info property is of type<br />

string . With the XElement class the string can be parsed <strong>and</strong> accessed by the LINQ to XML classes from<br />

the namespace System.Xml.Linq . Invoking the method Element() passing the name of the XML element<br />

Exam returns an XElement object that is then used to access the values of the attribute Number <strong>and</strong> the<br />

elements Title <strong>and</strong> Course in a much simpler way, as was done earlier with the XmlDocument class:<br />

using System;<br />

using System.Xml.Linq;<br />

namespace Wrox.ProCSharp.SqlServer<br />

{<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!