XmlSerialization to StringBuilder without Header information

  • Thread starter Thread starter ropo
  • Start date Start date
R

ropo

I am currently serializing an object called Value to and StringBuilder
as follows:

XmlSerializer Serializer = new XmlSerializer(Value.GetType());

StringBuilder sb = new StringBuilder();

StringWriter Sw = new StringWriter(sb);

XmlWriterSettings xmlWS = new XmlWriterSettings();

XmlWriter ValWriter = XmlWriter.Create(Sw, xmlWS);

Serializer.Serialize(ValWriter, Value);

This works but add the header information <?xml version="1.0"
encoding="utf-16"?> to the start of my StringBuilder, I don't require
this information because later on the whole thing is written as part
of an XML document which has its own header.

I tried inserting this:

xmlWS.ConformanceLevel = ConformanceLevel.Fragment;


But that causes an exception during writing, it appears XmlSerializer
requires ConformanceLevel.Document.


Later I deserialize out of the builder object as follows:

XmlSerializer Serializer = new
XmlSerializer(Type.GetType(strTypeName));
StringReader Sr = new System.IO.StringReader(Node.FirstChild.Value);
XmlReader Reader = System.Xml.XmlReader.Create(Sr);
objVal = Serializer.Deserialize(Reader);



Anyone known how I can get rid of the header without causing
exceptions during serialization?
 
I am currently serializing an object called Value to and StringBuilder
as follows:

XmlSerializer Serializer = new XmlSerializer(Value.GetType());

StringBuilder sb = new StringBuilder();

StringWriter Sw = new StringWriter(sb);

XmlWriterSettings xmlWS = new XmlWriterSettings();

XmlWriter ValWriter = XmlWriter.Create(Sw, xmlWS);

Serializer.Serialize(ValWriter, Value);

This works but add the header information <?xml version="1.0"
encoding="utf-16"?> to the start of my StringBuilder, I don't require
this information because later on the whole thing is written as part
of an XML document which has its own header.

I tried inserting this:

xmlWS.ConformanceLevel = ConformanceLevel.Fragment;

But that causes an exception during writing, it appears XmlSerializer
requires ConformanceLevel.Document.

Later I deserialize out of the builder object as follows:

XmlSerializer Serializer = new
XmlSerializer(Type.GetType(strTypeName));
StringReader Sr = new System.IO.StringReader(Node.FirstChild.Value);
XmlReader Reader = System.Xml.XmlReader.Create(Sr);
objVal = Serializer.Deserialize(Reader);

Anyone known how I can get rid of the header without causing
exceptions during serialization?

I've got around this problem by removing the header information after
serialization, which isn't pretty, but it works. Any more elegant
solutions will be welcome.
 
Back
Top