EDN Admin
Well-known member
<table>
<tbody>
<tr>
<td>
</td>
<td>
I have a piece of code that uses the
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
? or will I just have to live with this? <br/>
</td>
</tr>
</tbody>
</table><hr class="sig Shawn
View the full article
<tbody>
<tr>
<td>
</td>
<td>
I have a piece of code that uses the
Code:
XmlSerialzer
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
</td>
</tr>
</tbody>
</table><hr class="sig Shawn
View the full article