Showing posts with label XmlWriter. Show all posts
Showing posts with label XmlWriter. Show all posts

Tuesday, November 24, 2009

XML Serializer: How to omit namespace and other redundant info

public static string SerializeType(Type aType, object aObject)
{
XmlSerializerNamespaces lXmlSerializerNamespaces = new XmlSerializerNamespaces();
lXmlSerializerNamespaces.Add("", "");
XmlSerializer lXmlSerializer = new XmlSerializer(aType);
XDocument lXDocument = new XDocument();
using (XmlWriter lXmlWriter = lXDocument.CreateWriter())
{
XmlWriterSettings lXmlWriterSettings = lXmlWriter.Settings;
lXmlWriterSettings.OmitXmlDeclaration = true;

lXmlSerializer.Serialize(lXmlWriter, aObject, lXmlSerializerNamespaces);
lXmlWriter.Close();
}
return lXDocument.ToString();
}

Thursday, October 1, 2009

Write Dataset to XML with WriteSchema

Serializing dataset ignores empty datatables

When you call lDataSet.WriteXml(aXMLFilePath), any data table without rows will be ignored. To include it (because table schema may still be important), use the WriteXml overload that writes to an xmlWriter and enable"WriteSchema".

I use XDocument that can create XmlWriter but I'm sure there are other ways.

XDocument lXDocument = new XDocument();
using (XmlWriter lXmlWriter = lXDocument.CreateWriter())
{
lMasterDataSet.WriteXml(lXmlWriter, XmlWriteMode.WriteSchema);
}
lXDocument.Save(@"c:\temp\PSReportServer_MasterDataSetWithSchema.xml");