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.

974 ❘ ChaPTer 34 .net prOGrAmminG with sQl server<br />

}<br />

}<br />

@"server=(local);database=ProCSharp;trusted_connection=true";<br />

var connection = new SqlConnection(connectionString);<br />

var comm<strong>and</strong> = connection.CreateComm<strong>and</strong>();<br />

comm<strong>and</strong>.Comm<strong>and</strong>Text = "SELECT Id, Number, Info FROM Exams";<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 />

XmlReader xmlReader = xml.CreateReader();<br />

StringBuilder courses = new StringBuilder("Course(s): ", 40);<br />

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

{<br />

if (xmlReader.Name == "Exam" && xmlReaderIsStartElement())<br />

{<br />

Console.WriteLine("Exam: {0}", xmlReader.GetAttribute("Number"));<br />

}<br />

else if (xmlReader.Name == "Title" && xmlReader.IsStartElement())<br />

{<br />

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

}<br />

else if (xmlReader.Name == "Course" && xmlReader.IsStartElement())<br />

{<br />

courses.AppendFormat("{0} ", xmlReader.ReadString());<br />

}<br />

}<br />

xmlReader.Close();<br />

Console.WriteLine(courses.ToString());<br />

Console.WriteLine();<br />

}<br />

reader.Close();<br />

Running the application, you will get the output as shown:<br />

Exam: 70-502<br />

Title: TS: Microsoft .<strong>NET</strong> Framework 3.5, Windows Presentation Foundation Application Development<br />

Course(s): 6460<br />

Exam: 70-562<br />

Title: TS: Microsoft .<strong>NET</strong> Framework 3.5, ASP.<strong>NET</strong> Application Development<br />

Course(s): 2310 6463<br />

Exam: 70-561<br />

Title: TS: Microsoft .<strong>NET</strong> Framework 3.5, ADO.<strong>NET</strong> Application Development<br />

Course(s): 2310 6464<br />

Instead of using the XmlReader class, you can read the complete XML content into the XmlDocument<br />

class <strong>and</strong> parse the elements by using the DOM model. The method SelectSingleNode() requires an<br />

XPath expression <strong>and</strong> returns an XmlNode object. The XPath expression //Exam looks for the Exam XML<br />

element inside the complete XML tree. The XmlNode object returned can be used to read the children of the<br />

represented element. The value of the Number attribute is accessed to write the exam number to the console,<br />

then the Title element is accessed <strong>and</strong> the content of the Title element is written to the console, <strong>and</strong> the<br />

content of all Course elements is written to the console as well:<br />

string connectionString =<br />

@"server=(local);database=SqlServerSampleDB;trusted_connection=true";<br />

var connection = new SqlConnection(connectionString);<br />

var comm<strong>and</strong> = connection.CreateComm<strong>and</strong>();<br />

comm<strong>and</strong>.Comm<strong>and</strong>Text = "SELECT Id, Number, Info FROM Exams";<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!