Missing XML document header with DataSet.WriteXml()

B

Bjorn Brox

Using Compact Framework 3.5

The application that is recieving XML files from my application
requires that the file starts with the line

<?xml version="1.0" encoding="windows-1252" ?>

How do I ensure that DataSet.WriteXml(x) outputs this header?

The code I use today is:

XmlTextWriter xml = new XmlTextWriter(tmp_filename,
Encoding.GetEncoding(1252));
ds.WriteXml(xml);
xml.Close();

But as you guess the Xml header is missing, probably because
the filesystem encoding is 1252 (I have also tried Encoding.Default)
 
G

Guest

Use XmlTextWriter xml = XmlTextWriter.Create(fileName, settings);

You can adjust the settings to include (actually set Omit to false) the
header.
 
B

Bjorn Brox

Use XmlTextWriter xml = XmlTextWriter.Create(fileName, settings);

You can adjust the settings to include (actually set Omit to false) the
header.
Got a warning on that one because XmlTextWriter.Create returns a
XmlWriter, not a XmlTextWriter.

XmlWriter xml = XmlTextWriter.Create(fileName, settings);

Anyhow, your solution helped.
Thanks!
 
B

Bjorn Brox

Casting is just a way of getting rid of compiler warnings instead of
fixing the error.

I only use cast on getting correct type from a universal variable type
like "object", but again - to be safe you should test if the type you
want to cast to is correct.
 
G

Guest

Unlike C/C++, in C# it will only do a type safe cast. The IO classes are
very, very often cast because a lot Get and Create methods come from
abstract or virtual implementations in their bases. This means that they
can't return the type of the child, but must return the parent, and it's up
to you to cast. Int this case you *know* it's an XmlTextWriter and can't be
anything else. It's one line of code. If you're overly paranoid you can use
an "as" directive and then check for null afterward to prevent an exception
in the cast, but again, in this case you know for certain that the return
type is convertible.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 

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