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.

Fragments and Parser ContextThe context <strong>for</strong> an <strong>XML</strong> parser consists of all the <strong>in</strong><strong>for</strong>mation that can be used tocustomize the way <strong>in</strong> which the parser works. Context <strong>in</strong><strong>for</strong>mation <strong>in</strong>cludes theencod<strong>in</strong>g character set, the DTD <strong>in</strong><strong>for</strong>mation needed to set all the default attributes andto expand entities, the namespaces, the language, and the white space handl<strong>in</strong>g.If you specify the <strong>XML</strong> fragment us<strong>in</strong>g a Str<strong>in</strong>gReader object, as shown <strong>in</strong> the previoussection, all elements of the parser context are set with default values. The parsercontext is fully def<strong>in</strong>ed by the XmlParserContext class. When <strong>in</strong>stantiat<strong>in</strong>g anXmlTextReader class to operate on a str<strong>in</strong>g, you use the follow<strong>in</strong>g constructor andspecify a parser context:public XmlTextReader(str<strong>in</strong>g xmlFragment,XmlNodeType fragType,XmlParserContext context);The xmlFragment parameter conta<strong>in</strong>s the <strong>XML</strong> str<strong>in</strong>g to parse. The fragType argument,on the other hand, represents the type of fragment. It specifies the type of the node atthe root of the fragment. Only Element, Attribute, and Document nodes are permitted.The XmlParserContext constructor has a few overloads. The one with the shortest listof arguments, shown here, is probably the overload you will use most often:public XmlParserContext(XmlNameTable nt,XmlNamespaceManager nsMgr,str<strong>in</strong>g xmlLang,XmlSpace xmlSpace);Creat<strong>in</strong>g a new parser context is as easy as runn<strong>in</strong>g the follow<strong>in</strong>g statements:NameTable table = new NameTable();table.Add("Author");XmlNamespaceManager mgr = new XmlNamespaceManager(table);mgr.AddNamespace("company", "urn:ThisIsMyBook");XmlParserContext context;context = new XmlParserContext(table, mgr, "en-US",XmlSpace.None);The first parameter to this XmlParserContext constructor is a NameTable object. Thename table is used to look up prefixes and namespaces as atomized str<strong>in</strong>gs. Forper<strong>for</strong>mance reasons, you also need to pass a NameTable object—which <strong>in</strong>herits fromthe abstract XmlNameTable class—when creat<strong>in</strong>g a new <strong>in</strong>stance of a namespacemanager class.NoteIf the namespace manager and the parser context happen to usedifferent NameTable objects, the XmlParserContext might not beable to recognize the namespaces brought <strong>in</strong> by the manager,result<strong>in</strong>g <strong>in</strong> an <strong>XML</strong> exception.43

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

Saved successfully!

Ooh no, something went wrong!