can you add the standalone attribute to the C# XmlSerializer and still keep the proper formatting?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<table>
<tbody>
<tr>
<td>



</td>
<td>


I have a piece of code that uses the
Code:
XmlSerialzer
class to stream out a collection of objects. I need to have the "Standalone" attribute in the Xml declaration but the XmlSerializer does not put it in. I have found some answers on how to get
the standalone attribute to appear and I have modified my code to do this.
<pre class="prettyprint //init local vars.
strErrorMessage = "";
Type T = Type.GetType(strObjectType);

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

//Add an empty namespace and empty value
ns.Add("", "");

//init the xml serializer
XmlSerializer xmlWriter = new XmlSerializer(T);
XmlWriterSettings xwsSettings = new XmlWriterSettings();
xwsSettings.Encoding = Encoding.UTF8;
xwsSettings.OmitXmlDeclaration = true;
xwsSettings.NewLineOnAttributes = true;
xwsSettings.ConformanceLevel = ConformanceLevel.Document;
try
{
///Open a stream to the file for writing
//TextWriter fileWriter = new StreamWriter(strFileName);
var writer = XmlWriter.Create(strFileName, xwsSettings);

writer.WriteRaw("<?xml version="1.0" encoding="UTF-8" standalone="yes"?>rn");

//serialize the configuration file object to an xml file.
xmlWriter.Serialize(writer, oFile, ns);
// fileWriter.Close();
}
catch (XmlException e)
{
strErrorMessage = e.Message;
return false;
}
return true;

[/code]
If i look at a file generated by the above code, i see that the xml declaration i wanted is inded on the first line, then everything else on the next line. It doesnt format it the way it did when I used just the XmlSerializer and the
TextWriter. Is there a way to do what I want and still get the formatting using the
Code:
XmlSerializer
? or will I just have to live with this? <br/>


</td>
</tr>
</tbody>
</table><hr class="sig Shawn

View the full article
 
Back
Top