small xml load/save howto

N

not_a_commie

Here are a few thoughts for loading and storing your xml using the
XmlDocument class:

First, don't ever insert the header manually. This is to be done by
the writer. In other words, don't do this:

document.AppendChild(document.CreateXmlDeclaration("1.0", "UTF-8",
null));

If you do that and then write your document to a stream with an
encoding other than UTF8, well uh, you're skrewed at that point. If
you do anything other than a document.Save(filename) at that point
your're skrewed. And if you weren't expecting the byte ordering marks
in whatever you're using to read that document, you're skrewed.

Instead, save your document to a file this way:

using(var writer = new XmlTextWriter(blah, blah)){
document.Save(writer); writer.Close(); }

That will automagically insert the appropriate xml header for you. And
it won't stick in byte ordering marks (a BOM) without you specifically
telling it to do so in the XmlTextWriter params.

If you want to write your document to a string, save to a the
StringWriter. It will automatically add an xml header of type UTF-16
and it won't stick in that nasty BOM. On top of that, strings in C#
are UTF16 -- that means your header actually matches the storage
medium. Amazing!

As a note, XmlDocument.Load* methods actually use the XmlTextReader
and StringReader stuff appropriately so there is little to be gained
by not using the XmlDocument.Load methods.
 
J

Jeroen Mostert

not_a_commie said:
Here are a few thoughts for loading and storing your xml using the
XmlDocument class:
As an aside, use XmlDocument only if you need to manipulate the document
through the DOM, or if you only ever expect to manipulate small documents
that you need easy access to. XmlDocument creates individual objects to
represent nodes and cannot do any processing until the entire document has
been read, both of which contribute to poor scaling for very large
documents. XPathNavigator is a lightweight alternative. For .NET 3.5, the
new XElement class introduced as part of LINQ to XML is unsurpassed in ease
of use (personally, I love it <= => this much).

Instead, save your document to a file this way:

using(var writer = new XmlTextWriter(blah, blah)){
document.Save(writer); writer.Close(); }
Use XmlWrite.Create() instead of directly instantiating an XmlTextWriter. It
provides more control over the output, supports more output targets directly
and, depending on the output target, can create specialized writers that are
more efficient than XmlTextWriter.
If you want to write your document to a string, save to a the
StringWriter. It will automatically add an xml header of type UTF-16
and it won't stick in that nasty BOM. On top of that, strings in C#
are UTF16 -- that means your header actually matches the storage
medium. Amazing!
And extremely inconvenient if you're writing the document to a string (or a
MemoryStream) as an intermediate step for writing it somewhere else later,
where the encoding will not be UTF-16. Luckily, you can use
XmlWriter.Create() to construct a writer that will not emit the XML declaration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top