views:

103

answers:

1

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.

A: 

I figured this out. The xml file I was attempting to deserialize contained the following attribute in the root element:

xmlns="tfs"

Therefore every element in the xml file was under this xml namespace.

My changes to the code were essentially putting the AcctSetup element under a separate xml namespace 'com'. Therefore in order to deserialize a file, the file would need the 'xmlns="tfs"' in the root element and a separate 'xmlns="com"' attribute on the acctsetup child attribute.

Since this was not the format of the file I was receiving on the parent elements above the AcctSetup element were deserialized into the class instance.

My solution was to move the 'acctsetup' element back into the 'tfs' namespace again. In my particular situation the only downside is a slight bit of code duplication - but everything works again.

CletusLoomis