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.

To produce these results, I created the GetXmlFileNodeLayout function. This functionscans the entire contents of the <strong>XML</strong> file and processes each node found along theway. Only two types of nodes are relevant <strong>for</strong> this example: the start and end tags ofElement nodes. The NodeType enumeration identifies these two types of nodesthrough the keywords Element and EndElement.private str<strong>in</strong>g GetXmlFileNodeLayout(str<strong>in</strong>g file){// Open the streamXmlTextReader reader = new XmlTextReader(file);// Loop through the nodesStr<strong>in</strong>gWriter writer = new Str<strong>in</strong>gWriter();str<strong>in</strong>g tabPrefix = "";while (reader.Read()){// Write the start tagif (reader.NodeType == XmlNodeType.Element){tabPrefix = new str<strong>in</strong>g('\t', reader.Depth);writer.WriteL<strong>in</strong>e("{0}",tabPrefix,reader.Name);}else{// Write the end tagif (reader.NodeType == XmlNodeType.EndElement){tabPrefix = new str<strong>in</strong>g('\t', reader.Depth);writer.WriteL<strong>in</strong>e("{0}",tabPrefix,reader.Name);}}}// Write to the output w<strong>in</strong>dowstr<strong>in</strong>g buf = writer.ToStr<strong>in</strong>g();writer.Close();// Close the stream29

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

Saved successfully!

Ooh no, something went wrong!