XmlSerializer write not all Namespaces

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hello,
im implement a c#.net 3.5 based desktop app.
my should have a xml based excel export.
now, i run into the following problem.
i try to add some custom namespaces to my serializer like...

<div style="color:black; background-color:white
<pre><span style="color:blue public <span style="color:blue static MemoryStream SerializeObjectToStream(Object obj, Type type) <br/>{ <br/><br/>XmlSerializerNamespaces customNamespace = <span style="color:blue new XmlSerializerNamespaces();<br/> customNamespace.Add(<span style="color:blue string.Empty, <span style="color:#a31515 "urn:schemas-microsoft-com:office:spreadsheet");<br/> customNamespace.Add(<span style="color:#a31515 "o", <span style="color:#a31515 "urn:schemas-microsoft-com:office:office");<br/>customNamespace.Add(<span style="color:#a31515 "x", <span style="color:#a31515 "urn:schemas-microsoft-com:office:excel");<br/>customNamespace.Add(<span style="color:#a31515 "ss", <span style="color:#a31515 "urn:schemas-microsoft-com:office:spreadsheet");<br/> customNamespace.Add(<span style="color:#a31515 "html", <span style="color:#a31515 "http://www.w3.org/TR/REC-html40");<br/>MemoryStream memoryStream = <span style="color:blue new MemoryStream();<br/> XmlSerializer xs = <span style="color:blue new XmlSerializer(type, <span style="color:#a31515 "urn:schemas-microsoft-com:office:spreadsheet"); <br/>XmlTextWriter xmlTextWriter = <span style="color:blue new XmlTextWriter(memoryStream, Encoding.UTF8);<br/> xmlTextWriter .Namespaces = <span style="color:blue true;<br/> xmlTextWriter .WriteProcessingInstruction(<span style="color:#a31515 "xml", <span style="color:#a31515 "version="1.0" encoding="utf-8"");<br/>xmlTextWriter .WriteProcessingInstruction(<span style="color:#a31515 "mso-application", <span style="color:#a31515 "progid="Excel.Sheet"");<br/>xs.Serialize(xmlTextWriter , obj, customNamespace);<br/>memoryStream = (MemoryStream)xmlTextWriter .BaseStream;<br/> <span style="color:blue return memoryStream;<br/> }<br/>[/code]

<br/>

but it ignores the first XmlSerializerNamespaces-Entry and looks like:

<div style="color:black; background-color:white
<pre><span style="color:blue <?<span style="color:#a31515 xml <span style="color:red version<span style="color:blue =<span style="color:black "<span style="color:blue 1.0<span style="color:black " <span style="color:red encoding<span style="color:blue =<span style="color:black "<span style="color:blue utf-8<span style="color:black "<span style="color:blue ?><br/><span style="color:blue <?<span style="color:#a31515 mso-application <span style="color:red progid<span style="color:blue =<span style="color:black "<span style="color:blue Excel.Sheet<span style="color:black "<span style="color:blue ?><br/><span style="color:blue <<span style="color:#a31515 ss<span style="color:blue :<span style="color:#a31515 Workbook <span style="color:red xmlns:x<span style="color:blue =<span style="color:black "<span style="color:blue urn:schemas-microsoft-com:office:excel" <br/>xmlns:o="urn:schemas-microsoft-com:office:office" <br/>xmlns:html="http://www.w3.org/TR/REC-html40"<br/>xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet<span style="color:black "<span style="color:blue ><br/>...<br/>[/code]

<br/>
the aim is, to get the following output.

<div style="color:black; background-color:white
<pre><span style="color:blue <?<span style="color:#a31515 xml <span style="color:red version<span style="color:blue =<span style="color:black "<span style="color:blue 1.0<span style="color:black "<span style="color:blue ?><br/><span style="color:blue <?<span style="color:#a31515 mso-application <span style="color:red progid<span style="color:blue =<span style="color:black "<span style="color:blue Excel.Sheet<span style="color:black "<span style="color:blue ?><br/><span style="color:blue <<span style="color:#a31515 Workbook <span style="color:red xmlns<span style="color:blue =<span style="color:black "<span style="color:blue urn:schemas-microsoft-com:office:spreadsheet<span style="color:black "<br/> <span style="color:red xmlns:o<span style="color:blue =<span style="color:black "<span style="color:blue urn:schemas-microsoft-com:office:office<span style="color:black "<br/> <span style="color:red xmlns:x<span style="color:blue =<span style="color:black "<span style="color:blue urn:schemas-microsoft-com:office:excel<span style="color:black "<br/> <span style="color:red xmlns:ss<span style="color:blue =<span style="color:black "<span style="color:blue urn:schemas-microsoft-com:office:spreadsheet<span style="color:black "<br/> <span style="color:red xmlns:html<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/TR/REC-html40<span style="color:black "<span style="color:blue ><br/>[/code]


i guess, this xml will be created when its possbile to write the default-namespace.
but how can it do that ?
thanks for your support

<br/>
<br/>

View the full article
 
Back
Top