XML Serialization without Attributes?

Hello,

I'm programming with Visual Studio 2005 for .net 2.0 in C#. Very often I
use XML Serilization [1].
Some of the output files can be edited by the user with an external
editor. What I don't like are some attributes which are inserted into
the fily by the serializer, and which may confuse the user.

Example:
| <?xml version="1.0"?>
| <Summary xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
|       <CurrentCQI>
|             <Incident>
|                   <CQI>
<snip>

I'd like to have something like that:
| <Summary>
|       <CurrentCQI>
|             <Incident>
|                   <CQI>
<snip>

The "?xml version" and xmlns attributes are not needed for
deserialization. XML files which were written manually without this
information are deserialized without problem.

Is there a possibility to tell the serializer to leave this data out of
the output file?

TIA,
Christian

[1] Here's the kind of code I use for serialization:

public object Data; // The object to be saved, set by a different method
private Type SType; // The type of the object
private FileStream DStream;    
private XmlSerializer XmlSer;  

SType = Data.GetType();         
XmlSer = new XmlSerializer(SType);    

DStream = new FileStream(p, FileMode.CreateNew);    
XmlSer.Serialize(DStream, Data);                    
0
11/7/2009 3:03:24 PM
dotnet.xml 7266 articles. 0 followers. Follow

3 Replies
2199 Views

Similar Articles

[PageSpeed] 14

Christian Treffler wrote:

> I'm programming with Visual Studio 2005 for .net 2.0 in C#. Very often I
> use XML Serilization [1].
> Some of the output files can be edited by the user with an external
> editor. What I don't like are some attributes which are inserted into
> the fily by the serializer, and which may confuse the user.
> 
> Example:
> | <?xml version="1.0"?>
> | <Summary xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
> |       <CurrentCQI>
> |             <Incident>
> |                   <CQI>
> <snip>
> 
> I'd like to have something like that:
> | <Summary>
> |       <CurrentCQI>
> |             <Incident>
> |                   <CQI>
> <snip>
> 
> The "?xml version" and xmlns attributes are not needed for
> deserialization. 

<?xml version="1.0"?> is not an attribute, it is the XML declaration. If 
you don't want that then serialize to an XmlWriter with the 
XmlWriterSettings specifying to omit the XML declaration e.g.

             XmlWriterSettings xws = new XmlWriterSettings();
             xws.OmitXmlDeclaration = true;
             xws.Indent = true;

             XmlSerializer ser = new XmlSerializer(typeof(Foo));

             using (XmlWriter writer = XmlWriter.Create(@"file.xml", xws))
             {
                 ser.Serialize(writer, fooInstance);
                 writer.Close();
             }
-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/
0
mahotrash (1777)
11/7/2009 4:46:55 PM
Christian Treffler wrote:

> I'm programming with Visual Studio 2005 for .net 2.0 in C#. Very often I
> use XML Serilization [1].
> Some of the output files can be edited by the user with an external
> editor. What I don't like are some attributes which are inserted into
> the fily by the serializer, and which may confuse the user.
> 
> Example:
> | <?xml version="1.0"?>
> | <Summary xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
> |       <CurrentCQI>
> |             <Incident>
> |                   <CQI>
> <snip>
> 
> I'd like to have something like that:
> | <Summary>
> |       <CurrentCQI>
> |             <Incident>
> |                   <CQI>
> <snip>
> 
> The "?xml version" and xmlns attributes are not needed for
> deserialization. XML files which were written manually without this
> information are deserialized without problem.

If you think you don't need them then you can get rid of the xmlns:xsi 
and xmlns:xsd attributes as follows:

             XmlSerializer ser = new XmlSerializer(typeof(Foo));
             XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
             ns.Add("", "");

             using (XmlWriter writer = XmlWriter.Create(@"file.xml", xws))
             {
                 ser.Serialize(writer, fooInstanceObject, ns);
                 writer.Close();
             }


-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/
0
mahotrash (1777)
11/7/2009 6:48:51 PM
Martin Honnen wrote:

> <?xml version="1.0"?> is not an attribute, it is the XML declaration. If 
> you don't want that then serialize to an XmlWriter with the 
> XmlWriterSettings
<snip>

Martin, thank you for your inputs. I implemented them and found that the
XmlWriterSettings provided some additional means to control the output. 
That was very useful.

Christian
0
11/9/2009 2:30:48 PM
Reply:

Similar Artilces: