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);
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/
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/
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