473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

small xml load/save howto

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.

Jul 25 '08 #1
1 1744
not_a_commie wrote:
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).

<snip>
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.

--
J.
Jul 25 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tony Lugg | last post by:
I have an application with a document management form. When users add documents to the form, I call the API function SHGetFileInfo to get the associated large and small icons for the file. These...
3
by: Tony Lugg | last post by:
I have an application with a document management form. When users add documents to the form, I call the API function SHGetFileInfo to get the associated large and small icons for the file. These...
2
by: Alejandro Lapeyre | last post by:
I am using a ImageList with a bitmap that contains all the images. I am currently saving the bitmap in a PictureBox, and load into the ImageList when the form loads. How can I save the bitmap...
1
by: Ron | last post by:
I need to write a little vb.net app that looks at XML files and manipulates text in them. Here is what I need to do and I have NO idea how to do something like this. here is an example file:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.