XmlSerialization to StringBuilder without Header information
am 12.11.2007 12:57:29 von 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
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?
Re: XmlSerialization to StringBuilder without Header information
am 12.11.2007 17:06:17 von ropo
On 12 Nov, 11:57, ropo wrote:
> 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
> 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.