XmlSerializer - suppression

  • Thread starter Thread starter Raghu
  • Start date Start date
R

Raghu

XmlSerializer object in .net framework allows serialization of objec type to
xml. However it also inputs the xml declaration string (<?xml version="1.0"
encoding="utf-8"?> into the output when serializing. Is there a way to
suppress this string?

Thanks.
Raghu/..
 
Raghu,

No, I don't believe there is. You will have to modify the output
yourself. Removing it would invalidate the XML document, so I can see why
that option is not there.

What are you trying to do?
 
I am using types that conform to xml schema as an input parameter to my web
method. The idea is to create client side classes to make it easier for
clients. Those objects can be populated and sent to web methods. In the web
method, I may have to serialize type info into xml and add my own wrapper
xml wrapper around it for further processing. When serializing the type,
since it contains the xml declaration . So it can not be wrapper in an outer
xml element as is. Hence my question.

Nicholas Paldino said:
Raghu,

No, I don't believe there is. You will have to modify the output
yourself. Removing it would invalidate the XML document, so I can see why
that option is not there.

What are you trying to do?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Raghu said:
XmlSerializer object in .net framework allows serialization of objec
type
to
xml. However it also inputs the xml declaration string (<?xml version="1.0"
encoding="utf-8"?> into the output when serializing. Is there a way to
suppress this string?

Thanks.
Raghu/..
 
I think that you can do that if you hand a text writer instance(which is
already initialized) to the serialization . for instance:

XmlSerializer ser = new XmlSerializer( typeof(myClass) );
FileStream stream = new FileStream( "MiXMlFile.xml". FileMode.OpenOrCreate);
XmlTextWriter writer = new XmlTextWriter( stream, new
System.Text.UTF8Encoding() );
writer.WriteStartElement( "TopElem", "http://Top" );
ser.Serialize( writer, myClassInstanceVariable);
writer.WriteEndElement();
writer.Close();

regards,
Luis Abreu
 
Hello, Raghu-

If you pass an XmlWriter to the serializer, it will only emit a processing
instruction if the XmlWriter's state is 'Start' (i.e., has not had anything
written to it yet).

For example:
// Assume we have a type named 'MyType' and a variable of this type named
'myObject'
System.Text.StringBuilder output = new System.Text.StringBuilder();
System.IO.StringWriter internalWriter = new System.IO.StringWriter(output);
System.Xml.XmlWriter writer = new System.Xml.XmlTextWriter(internalWriter);
System.Xml.Serialization.XmlSerializer serializer = new
System.Xml.Serialization.XmlSerializer(typeof(MyType));

writer.WriteStartElement("MyContainingElement");
serializer.Serialize(writer, myObject);
writer.WriteEndElement();

In this case, the writer will be in a state of 'Element' (inside an element)
so no processing instruction will be written. One you finish writing the
XML, you can extract the text from the underlying stream and process it to
your heart's content.

Hope this helps!
-Andy Hopper
http://www.dotnetified.com
 
Back
Top