The Problem
I have an issue when I deserialize my xml, one of the child elements is not being deserialized. It is null in the class instance despite being populated in the xml.
A little Background
I used XsdObjectGenerator to create poco classes based on my .xsd. I was then able to deserialize the xml into a class instance and work with the objects.
I modifed the autogenerated classes by wrapping them in a namespace called 'TFS'.
At a certain point in the project, I found it necessary to place one of the child elements in it's own namespace called 'COM'.
The next time I attempted to deserialized the xml into a class instance, the child element is not being deserialized.
Here is the pertinent snippet of TFS class code with the child element namespace changed to COM. It also includes the parent element OrdDtl which remains in the TFS namespace.
[XmlRoot(ElementName="OrdDtl",Namespace=Declarations.SchemaVersion,IsNullable=false),Serializable]
public class OrdDtl
{
[XmlElement(Type=typeof(COM.AcctSetup), ElementName="AcctSetup", IsNullable=false, Form=XmlSchemaForm.Qualified, Namespace = Declarations.SchemaVersion)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public COM.AcctSetup __AcctSetup;
[XmlIgnore]
public COM.AcctSetup AcctSetup
{
get
{
if (__AcctSetup == null) __AcctSetup = new COM.AcctSetup();
return __AcctSetup;
}
set {__AcctSetup = value;}
}
Here is the AcctSetup declaration in the COM namespace which is contained in it's own file:
[XmlRoot(ElementName = "AcctSetup", Namespace = Declarations.SchemaVersion, IsNullable = false), Serializable]
public class AcctSetup
{
......
}
and here is my deserialize function:
public static T XMLStringToXMLObject<T>(string pXmlString)
{
T retVal = default(T);
try
{
XmlSerializer vSerializer = new XmlSerializer(typeof(T));
MemoryStream vMemoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlString));
XmlTextWriter vXmlTextWriter = new XmlTextWriter(vMemoryStream, Encoding.UTF8);
retVal = (T)vSerializer.Deserialize(vMemoryStream);
}
catch (System.Exception ex)
{
if (ExceptionMain.LogException(ex))
throw;
}
return retVal;
}
Any assistance in getting the COM.AcctSetup element to deserialize is very much appreciated. Let me know if you need additional info or code samples.