13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

The method we create to do this is the user-def<strong>in</strong>ed function GetAttributeList.GetAttributeList takes a reference to the reader and extracts attribute values <strong>for</strong> thecurrently selected node.// Assume we call this method after hav<strong>in</strong>g read the nodestr<strong>in</strong>g GetAttributeList(XmlReader reader){Str<strong>in</strong>g buf = "";if (reader.HasAttributes)while(reader.MoveToNextAttribute())buf += reader.Name + "=\""+ reader.Value + "\" ";}reader.MoveToElement();return buf;When the po<strong>in</strong>ter is not already positioned on an attribute node, call<strong>in</strong>gMoveToNextAttribute is equivalent to call<strong>in</strong>g MoveToFirstAttribute, which moves thepo<strong>in</strong>ter to the first attribute node.An <strong>XML</strong> reader can move only <strong>for</strong>ward, which means that no previously visited nodecan be revisited once you have moved on to another node. This rule has a very specificexception. When the po<strong>in</strong>ter is positioned on an attribute node, you can move back tothe parent node us<strong>in</strong>g the MoveToElement method. This exception exists because,after all, an attribute is a particular type of node that is used to qualify the contents ofthe parent. From this po<strong>in</strong>t of view, an attribute is seen as a sort of subnode, andmov<strong>in</strong>g between the attributes of a given node does not logically change the <strong>in</strong>dex ofthe current element node. Us<strong>in</strong>g MoveToAttribute and MoveToFirstAttribute, you canjump from one attribute node to the next <strong>in</strong> both directions.Pars<strong>in</strong>g Mixed-Content AttributesNormally, the content of an attribute consists of a simple str<strong>in</strong>g of text. If you need touse it as an <strong>in</strong>stance of a more specific type (<strong>for</strong> example, a date or a Boolean value),you can convert the str<strong>in</strong>g us<strong>in</strong>g either the methods of the static classes XmlConvert(recommended) or even System.Convert.In some situations, however, the content of an attribute is mixed and <strong>in</strong>cludes pla<strong>in</strong> textalong with entities. Although unable to resolve entity references, the XmlTextReaderclass can separate text from entities when both are embedded <strong>in</strong> an attribute's value.For this to happen, you must parse the attribute's content us<strong>in</strong>g the ReadAttributeValuemethod <strong>in</strong>stead of simply read<strong>in</strong>g the content via the Value property.The follow<strong>in</strong>g code demonstrates how to rewrite the GetAttributeList function so that itcan preprocess mixed attributes and separate text from entities. The added code isshown <strong>in</strong> boldface.// Assume we call this method after hav<strong>in</strong>g read the nodestr<strong>in</strong>g GetAttAttributeList(XmlReader reader){Str<strong>in</strong>g buf = "";if (reader.HasAttributes)while(reader.MoveToNextAttribute()){33

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

Saved successfully!

Ooh no, something went wrong!