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.

epresents the character that will be used to <strong>in</strong>dent the text of the new node. By default,<strong>for</strong>matt<strong>in</strong>g is on and the <strong>in</strong>dentation is two blanks.Note that all the <strong>XML</strong> writer's <strong>for</strong>matt<strong>in</strong>g is managed by the writer only be<strong>for</strong>e thedocument is actually opened—that is, prior to the WriteStartDocument call. Thefollow<strong>in</strong>g code snippet demonstrates how to write a new <strong>XML</strong> document, <strong>in</strong>dent<strong>in</strong>g witha tab character any level of the hierarchy:XmlTextWriter writer = new XmlTextWriter(filename, null);writer.Formatt<strong>in</strong>g = Formatt<strong>in</strong>g.Indented;writer.Indentation = 1;writer.IndentChar = "\t";As a f<strong>in</strong>al note, keep <strong>in</strong> m<strong>in</strong>d that <strong>XML</strong> <strong>for</strong>matt<strong>in</strong>g normally <strong>in</strong>dents element contentsonly and does not <strong>for</strong>mat mixed contents.Support<strong>in</strong>g NamespacesIn the XmlTextWriter class, all the methods available <strong>for</strong> writ<strong>in</strong>g element nodes andattributes have overloads to work with namespaces. You simply add a new argument tothe call and specify the namespace prefix of choice. A namespace is identified by aURN and is used to qualify both attribute and node names so that they belong to aparticular doma<strong>in</strong> of names.Namespace DeclarationYou <strong>in</strong>sert a namespace declaration <strong>in</strong> the current node us<strong>in</strong>g the xmlns attribute. Youcan also optionally specify a namespace prefix. The prefix is a symbolic name thatuniquely identifies the namespace. To declare a namespace, add a special attribute tothe node that roots the target scope of the namespace, as shown here:You can write this <strong>XML</strong> text as raw text or use one of the methods of the writer object.Typically, you use one of the overloads of the WriteAttributeStr<strong>in</strong>g method, as shownhere:public void WriteAttributeStr<strong>in</strong>g(str<strong>in</strong>g prefix,str<strong>in</strong>g attr,str<strong>in</strong>g ns,str<strong>in</strong>g value);You can use this method to declare a namespace, but it rema<strong>in</strong>s primarily a method toadd attributes. To obta<strong>in</strong> a namespace declaration like the one <strong>in</strong> our earlier examples,a few exceptions to the signature apply. In particular, <strong>for</strong> an xmlns attribute be<strong>in</strong>gwritten, you <strong>in</strong>struct the method to add an attribute whose name matches the prefix andwhose prefix equals xmlns.The third argument is expected to be the URN of the namespace <strong>for</strong> the attribute. Inthis case, however, the namespace prefix named xmlns po<strong>in</strong>ts to the default <strong>XML</strong>namespace, so the ns argument must be set to null. Note that any attempt to set ns to anon-null value would result <strong>in</strong> an exception because the specified URN would not matchthe URN of the xmlns namespace prefix. The fourth and f<strong>in</strong>al argument, value, conta<strong>in</strong>sthe URN of the namespace you are declar<strong>in</strong>g. The follow<strong>in</strong>g code shows how to declarea sample namespace rooted <strong>in</strong> the node :writer.WriteStartElement("MyNode");writer.WriteAttributeStr<strong>in</strong>g("xmlns", "x", null,129

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

Saved successfully!

Ooh no, something went wrong!