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.

Figure 4-3: A dynamically created <strong>XML</strong> document with entities and DOCTYPE def<strong>in</strong>itions.If you need to concatenate entities with pla<strong>in</strong> text or if you just want to write thecontents of an attribute, use the WriteStr<strong>in</strong>g method. For example, the follow<strong>in</strong>g codeadds ", Europe" to the attribute Country:writer.WriteStartAttribute("Country", "");writer.WriteEntityRef("I");writer.WriteStr<strong>in</strong>g(", Europe");writer.WriteEndAttribute();Figure 4-4 shows the results of the concatenation.Figure 4-4: The Country attribute is created by concatenat<strong>in</strong>g an entity reference and pla<strong>in</strong>text.As you might have noticed, the end tags <strong>for</strong> both attributes and nodes do not take anyarguments. The writer ma<strong>in</strong>ta<strong>in</strong>s an <strong>in</strong>ternal stack of opened attributes and nodes andautomatically pops the <strong>in</strong>nermost element when you close a node. Likewise, when anew node or attribute is opened, the writer simply pushes a new element onto thestack. If the newly added element is a node, <strong>in</strong> the result<strong>in</strong>g <strong>XML</strong> code, the node isnested one additional level.At the end of the document—that is, when WriteEndDocument is called—all pend<strong>in</strong>gnodes are automatically popped off the stack and closed accord<strong>in</strong>g to the last <strong>in</strong>, firstout (LIFO) method. Let's consider what can happen if you disregard this simple rule andomit a call to WriteEndElement <strong>in</strong> a loop. The follow<strong>in</strong>g code translates an array ofstr<strong>in</strong>gs <strong>in</strong>to <strong>XML</strong>:126

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

Saved successfully!

Ooh no, something went wrong!