473,769 Members | 4,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Append Child(document. CreateXmlDeclar ation("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(f ilename) 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(b lah, blah)){
document.Save(w riter); 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.Loa d* methods actually use the XmlTextReader
and StringReader stuff appropriately so there is little to be gained
by not using the XmlDocument.Loa d methods.

Jul 25 '08 #1
1 1768
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(b lah, blah)){
document.Save(w riter); 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.Creat e() 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
1496
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 icons are added to two ImageList objects which are bound to a ListView control, and everything looks great. I am saving the icons to a SQL Server table by using Icon.Save to a stream and assigning the byte array to the field, then loading them...
3
2758
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 icons are added to two ImageList objects which are bound to a ListView control, and everything looks great. I am saving the icons to a SQL Server table by using Icon.Save to a stream and assigning the byte array to the field, then loading them...
2
8750
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 into a Resource and load it from there later? Thanks. Alejandro Lapeyre
1
1234
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: also can be found at: http://www.keepitsimplekid.com/xml/Ad00304.xml <XMD-entity ENTITY_TYPE="Ad" PAGE_NO="3" ID="Ad00304" BOX="450 1030 1229 1429" LANGUAGE="English" SNP="Ad00304S.jpg" SNP_WIDTH="156" SNP_HEIGHT="80"> <Meta NAME="Untitled Ad"...
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9850
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7396
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.