Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Use Multiple Readers?

In some situations, you may have one reader that reads part of an XML document, and one or more other readers that read the rest of the document. For example, an e-commerce ordering system may have orders submitted as XML documents. In processing these orders, one reader may process information at the beginning of the file and then route the rest of the document to another reader to handle the remaining order details.

VB MultipleXmlReader.exe
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\Xml\MultipleXmlReader\]

The following sample code creates an XmlReader that reads the first portion of the document.

FileStream filestreamSource = new FileStream(args, FileMode.Open, FileAccess.Read);
//Create XmlReader			
reader = XmlReader.Create(filestreamSource,"http://tempuri.org/unknown", Encoding.UTF8, null, new XmlReaderSettings() );

The second reader reads the remaining part of the document. The second reader is an XmlTextReader constructed using the XmlParserContext class. The code first creates an XmlNamespaceManager, and then adds the prefixes and namespaces (including the default namespace) used in the XML document to the XmlNamespaceManager. The XmlParserContext used to construct the XmlTextReader then uses this XmlNamespaceManager as the namespace manager to use to look up namespace information.

XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable);
//Set default namespace--first param is null.
nsmanager.AddNamespace(String.Empty, "http://tempuri.org/mydefaultnamespace");
nsmanager.AddNamespace("myns", "http://tempuri.org/mynamespace");
nsmanager.AddNamespace("yourns", "http://tempuri.org/yournamespace");
XmlParserContext pc = new XmlParserContext(reader.NameTable, nsmanager, reader.XmlLang, 

// Reset the filestream to beginning of the source stream
filestreamSource.Seek(0, SeekOrigin.Begin);
XmlTextReader reader2 = new XmlTextReader(filestreamSource, XmlNodeType.Element, pc);


Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.