Question

I need to send a request to the web service. this request asks for a "session" object as parameter.

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18060")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.xmlns.cfins.com/cfins/services/publishPolicyData/1.0")]
public partial class session {

    private object[] itemsField;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("data", typeof(sessionData))]
    [System.Xml.Serialization.XmlElementAttribute("properties", typeof(sessionProperties))]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
}

here's my xml file.

    <session id="6055">
      <properties dateModified="2014-01-08" engineVersion="2.0" cultureCode="en-US" cultureName="United States [english]">
        <userName>admin</userName>
      </properties>
      <data id="d2311A049FDC64CD9AE9EF3DE5874CB2D">
        .....
      </data>
    </session>

when I deserialize this xml file, I only get id, items is alway null. BTW, the session class is from web service, I cannot change it.

    public static session GetSession()
    {
        var s = new session();
        var data = new sessionData();

        var sessionXml = @"C:\Projects\CFWinSvc - Copy\XmlDeSerialize\session.xml";

        using (TextReader reader = new System.IO.StreamReader(sessionXml))
        {
            var serializer = new XmlSerializer(typeof(session));
            s = (session)serializer.Deserialize(reader);
        }

        return s;

    }


    public static session GetSession()
    {
        var s = new session();
        var data = new sessionData();


        var sessionXml = @"C:\Projects\CFWinSvc - Copy\XmlDeSerialize\session.xml";

        using (TextReader reader = new System.IO.StreamReader(sessionXml))
        {
            var serializer = new XmlSerializer(typeof(session));
            s = (session)serializer.Deserialize(reader);
        }

        return s;

    }

UPDATE:

here's the XML Schema.

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns="http://www.cfins.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xmlns.cfins.com/cfins/services/publishPolicyData/1.0" elementFormDefault="qualified" attributeFormDefault="unqualified">
        <xs:element name="session">
            <xs:complexType>
                <xs:sequence>
                    <xs:choice maxOccurs="unbounded">
                        <xs:element name="properties">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="userName" type="xs:string"/>
                                </xs:sequence>
                                <xs:attribute name="dateModified" use="optional"/>
                                <xs:attribute name="engineVersion" type="xs:string" use="optional"/>
                                <xs:attribute name="cultureCode" type="xs:string" use="optional"/>
                                <xs:attribute name="cultureName" type="xs:string" use="optional"/>
                            </xs:complexType>
                        </xs:element>
                        <xs:element name="data">
                            <xs:complexType>
                                <xs:all>
                                    <xs:element name="AlwaysAddIDs" type="xs:boolean" minOccurs="0"/>
                                    <xs:element name="ProRatingOn" type="xs:boolean" minOccurs="0"/>
                                    <xs:element name="carrier" minOccurs="0">
                                        <xs:complexType>
                                            <xs:attribute name="id" type="xs:string" use="optional"/>
                                        </xs:complexType>
                                    </xs:element>
                                    <xs:element name="userinterface" minOccurs="0">
                                        <xs:complexType>
                                            <xs:attribute name="id" type="xs:string" use="optional"/>
                                        </xs:complexType>
                                    </xs:element>
                                </xs:all>
                                <xs:attribute name="id" type="xs:string" use="optional"/>
                            </xs:complexType>
                        </xs:element>
                    </xs:choice>
                </xs:sequence>
                <xs:attribute name="id" use="optional"/>
            </xs:complexType>
        </xs:element>
    </xs:schema>
Was it helpful?

Solution

There is nothing wrong with your session class. However, it is marked as

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.xmlns.cfins.com/cfins/services/publishPolicyData/1.0")]

which pretty much means that xml deserializer will only consider sub-elements of your session that are in the same xml namespace.

In short, adding ugly namespace definitions to your xml file like so:

<properties xmlns="http://www.xmlns.cfins.com/cfins/services/publishPolicyData/1.0" ...

will do the trick: you'll get all your sessionProperties and sessionData where needed.

OTHER TIPS

XmlElement should not be used for collection or array properties. XmlArray and XmlArrayItem should be used instead. I don't know why the code would be generated like you show above. Did you modify it after it was generated? If you share your schema, maybe we can identify the issue with the codegen.

[XmlArray]
[XmlArrayItem("data", typeof(sessionData))]
[XmlArrayItem("properties", typeof(sessionProperties))]
public object[] Items {
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}

To use Xml Serialization, all your attribute need to have a public getter and setter, else the reflector while refuse to serialize or deserialize your object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top