Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Accessing and Manipulating XML Data

Accessing and Manipulating XML Data



|Views: 1,139|Likes:
Published by iksingh05

More info:

Published by: iksingh05 on Jan 29, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF or read online from Scribd
See more
See less


Chapter 6Accessing and Manipulating XML DataAbout This ChapterXML is central to data exchange between applications in the .NETFramework. You need to know the XML Document Object Model (DOM) andhow to access XML data in your applications. In this lesson you willlearn how to read and write data from XML documents, perform querieson XML documents, and validate XML documents with XML Schema. Inaddition, you will also learn to populate a DataSet with data from anXML file and write data from a DataSet into an XML file.Before You BeginTo complete the lessons in this chapter, you
Must have knowledge of programming in Microsoft Visual Basic .NETor Microsoft Visual C#
Must have a working knowledge of XMLLesson 1: Understanding the XML Document Object ModelYou need to know the XML Document Object Model (XML DOM) to work withXML data in your applications. In this lesson, you will learn aboutthe XML DOM and what constitutes a well-formed XML document. You willalso learn how to use the objects available in the XML DOM to read andwrite XML data. Finally, you will learn how to create an XML Parser inVisual Studio .NET.After this lesson, you will be able to
Describe the XML Document Object Model
Use the XML Document Object Model to read and write XML data
Create an XML Parser in Visual Studio .NETEstimated lesson time: 30 minutesOverview of the XML Document Object ModelTo enable disparate systems to communicate with each other, yourequire a standard that is understandable by all systems. Therefore,the obvious choice is a standard that is text-based. Because mostsystems understand text-based data, XML is the preferred standard ofcommunication. XML files conform to the standards developed by theWorld Wide Web Consortium (W3C). Let's take a look at a well-formedXML document.XML Document
<?xml version= "1.0"?><employees>
Page 1 of 53Accessing and Manipulating XML Data1/24/2005file://C:\Documents%20and%20Settings\vimandi\Local%20Sett...
<employee><FirstName>John</FirstName><LastName>Doe</LastName><DateOfBirth>08/09/1968</DateOfBirth><DateOfJoining>04/01/1992</DateOfJoining><Address>2010 Stanley Dr., Charlotte, NC 28273</Address><Basic>2100</Basic><Designation>Associate Consultant</Designation><LeaveBalance>12</LeaveBalance></employee><employee><FirstName>Luis</FirstName><LastName>Bonifaz</LastName><DateOfBirth>01/12/1972</DateOfBirth><DateOfJoining>06/01/2000</DateOfJoining><Address>7862 Freepoint Pkwy, Tampa, FL 33624</Address><Basic>1400</Basic><Designation>Developer</Designation><LeaveBalance>4</LeaveBalance></employee></employees>
The preceding XML code contains information about two employees and isa well-formed XML document. A well-formed XML document contains an endtag for every begin tag. For example, for every <employee> tag, a</employee> tag should be present. A well-formed XML document can havean associated document type definition (DTD) or an XML Schema thatdescribes the data and the relationship between the data within an XMLdocument. DTDs define the grammar for a class of XML documents. DTDshave a syntax that is different from the syntax of XML. XML Schema, onthe other hand, is an XML document that describes the elements andattributes of an XML document and can include type information. Youuse DTDs or XML Schema to describe and validate XML documents.Validating XML documents is discussed in detail in Lesson 5.To read XML documents, your application should be able to decipher theway in which XML documents are formed. The XML Document Object Modelallows you to read, manipulate, and modify an XML documentprogramatically.The XML Document Object ModelThe XML Document Object Model (XML DOM) class is a representation ofthe XML document in memory. The DOM class lets you read, write, andmanipulate an XML document. The .NET Framework provides classes, whichenable you to navigate through an XML document and obtain relevantinformation. Every XML document consists of parent and child nodes. Inthe XML document presented earlier, <employees> is the parent node.The immediate child node of the <employees> tag is the <employee>node. The <employee> node has many child nodes. When nodes are in thesame level, such as the <FirstName> and <LastName> nodes in theexample document, they are known as siblings.The XML DOM contains different types of nodes. Table 6.1 lists themost commonly used nodes and their descriptions.Table 6-1. Nodes in the XML DOMPage 2 of 53Accessing and Manipulating XML Data1/24/2005file://C:\Documents%20and%20Settings\vimandi\Local%20Sett...
You can also use Simple API for XML (SAX) to read data from XMLdocuments. Unlike DOM, SAX does not load the entire XML document intomemory. Rather, the SAX parser reads the contents sequentially andgenerates events as it reads the XML document. Because SAX does notload the XML document into memory, it is good for reading large XMLdocuments. However, one of the limitations of SAX is that it does notmaintain any data structures that are required to perform complexsearches. In addition, you cannot use SAX when you want to modify theXML ?document.Another way in which you can work with XML documents is by using the ?XmlReader class of the .NET Framework. The XmlReader class providesread-only, forward-only access to XML documents. Unlike XML DOM, theXmlReader class does not load the XML document into memory.Before you learn about various objects that you use to read and editan XML ?document using the XML DOM, you should learn how the XMLdocument is structured in memory. The root node is the document nodeand is represented by the ?XmlDocument object. The XmlDocument objectis derived from the XmlNode class. The XmlDocument object is used toperform tasks such as loading and saving XML documents. TheXmlDocument object consists of the Load, LoadXml, and Save methods,which enable you to load and save XML documents. In addition, you canuse the XmlDocument object to access all the nodes in the document.The following code snippet shows how to load an XML document into theXML DOM.Visual Basic .NET
Imports SystemImports System.IOImports System.XmlModule Module1Sub Main()Dim myDocument As New XmlDocument()myDocument.Load("emp.xml")Console.WriteLine(myDocument.InnerXml.ToString)Console.ReadLine()End SubEnd Module
DOM nodetypeDescriptionDocumentThis node type is the container for all the nodes and isalso known as the document root.DocumentType This node type represents the <!DOCTYPE> node.Element This node type represents element nodes.AttributeThis node type represents the attributes of an elementnode.Comment This node type represents comment nodes.TextThis node type represents the text that belongs to aparticular node or to an attribute.Page 3 of 53Accessing and Manipulating XML Data1/24/2005file://C:\Documents%20and%20Settings\vimandi\Local%20Sett...

Activity (2)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->