I created a custom class using Add-Type and it's defined by C# code. Because of the limitations of the export and import CliXml cmdlets I created a serialize and deserialize method on the class. This works great from my C# project in VS however when I run it from Powershell it throws the following exception:
There was an error generating the XML document. at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces) at ConfigMgr.Serialize() The type initializer for 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterConfigMgr' threw an exception. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterConfigMgr..ctor() at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializerContract.get_Writer() at System.Xml.Serialization.TempAssembly.InvokeWriter(XmlMapping mapping, XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
Any ideas what this means?
Here's the class definition, including the serialization method:
[Serializable]
public class ConfigMgr
{
public ConfigMgr()
{
Locations = new List<Location>();
}
[XmlArray]
public List<Location> Locations{get; set;}
[XmlIgnore]
public LocCfg CurrentConfig
{
get
{
foreach(Location pcl in Locations)
{
foreach(LocCfg pc in pcl.Configs)
{
if(pc.CurrentLoc)
{
return pc;
}
}
}
return null;
}
set
{
foreach(Location pcl in Locations)
{
foreach(LocCfg pc in pcl.Configs)
{
if(pc.CurrentLoc)
{
using(System.IO.StreamReader myFile = new System.IO.StreamReader(pcl.PhysicalPath + "\\settings.config"))
{
pc.Settings = myFile.ReadToEnd();
}
using(System.IO.StreamReader myFile = new System.IO.StreamReader(pcl.PhysicalPath + "\\keystore.config"))
{
pc.KeyStore = myFile.ReadToEnd();
}
}
if(pc.Equal(value))
{
pc.CurrentLoc = true;
}
else
{
pc.CurrentLoc = false;
}
}
}
}
}
public string Serialize()
{
try{
StringBuilder strRequest = new StringBuilder();
XmlWriterSettings xs = new XmlWriterSettings();
xs.OmitXmlDeclaration = true;
using (XmlWriter xwRequest = XmlWriter.Create(strRequest, xs))
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
XmlSerializer serRequest = new XmlSerializer(typeof(ConfigMgr));
ns.Add("", "");
serRequest.Serialize(xwRequest, this, ns);
}
return strRequest.ToString();
}
catch(Exception ex)
{
string err = "";
err = ex.Message + "\n" + ex.StackTrace;
Exception exIn = ex.InnerException;
while (exIn != null)
{
err += "\n\n" + ex.InnerException.Message + "\n" + ex.InnerException.StackTrace;
exIn = exIn.InnerException;
}
return err;
}
}
public static ConfigMgr Deserialize(string filepath)
{
using (XmlReader reader = XmlReader.Create(filepath))
{
XmlSerializer serRequest = new XmlSerializer(typeof(ConfigMgr));
if (serRequest.CanDeserialize(reader))
{
try
{
ConfigMgr ad = (ConfigMgr)serRequest.Deserialize(reader);
XmlDocument x = new XmlDocument();
return ad;
}
catch
{
return new ConfigMgr();
}
}
else
{
return new ConfigMgr();
}
}
}
}