views:

59

answers:

2

I am a newbie to C#. I have a java REST service which returns a xml response and I am trying to deserialize the xml document using C# XmlSerializer. A sample xml document response is pasted below.

<?xml version="1.0" encoding="UTF-8"
 standalone="yes" ?>  <ns2:Document
 xmlns:ns2="http://hxps.honeywell.com/model/impl"
 xmlns:ns3="http://hxps.honeywell.com/datatypes/impl"
 type="PS">
 <docId>SamplePSDocument1</docId> 
 <fields
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:ns5="http://jaxb.dev.java.net/array"
 xsi:type="ns5:anyTypeArray">   <item
 xsi:type="ns3:scalarFieldImpl"
 key="Name">
      <value xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xsi:type="xs:string">HXPS A4</value>
 </item>   <item
 xsi:type="ns3:scalarFieldImpl"
 key="Creation Date">
       <value xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xsi:type="xs:string">20 April
 2007</value>    </item> </fields>
 <id>fb92f871-1f3d-4fa4-ba24-5ae3af0a493f</id>
 <revision>1-c75f688e212fb5341ebdbd22a3867c14</revision>

 - <version>   <majorVersionNumber>1</majorVersionNumber>
 <minorVerisonNumber>5</minorVerisonNumber>
 </version> </ns2:document>

It works fine when I deserialize this xml document into Document object. My document class is pasted below

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://hxps.honeywell.com/model/impl", TypeName = "PSDocument")]
[System.Xml.Serialization.SoapTypeAttribute(Namespace = "http://hxps.honeywell.com/model/impl", TypeName = "PSDocument")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://hxps.honeywell.com/model/impl", ElementName = "Document")]

public partial class PSDocument
{

    private Com.Honeywell.Hxps.Sdk.Model.DocumentType _type;
    private string _description;
    private string _displayName;
    private string _docId;
    private Com.Honeywell.Hxps.Sdk.Model.Impl.VersionImpl _version;
    private object _fields;
    private string _revision;
    private string _id;
    /// <summary>
    ///  (no documentation provided)
    /// </summary>
    [System.Xml.Serialization.XmlAttributeAttribute(AttributeName = "type")]
    [System.Xml.Serialization.SoapAttributeAttribute(AttributeName = "type")]
    public Com.Honeywell.Hxps.Sdk.Model.DocumentType Type
    {
        get
        {
            return this._type;
        }
        set
        {
            this._type = value;
        }
    }

    /// <summary>
    ///  Property for the XML serializer indicating whether the "Type" property should be included in the output.
    /// </summary>
    [System.Xml.Serialization.XmlIgnoreAttribute]
    [System.Xml.Serialization.SoapIgnoreAttribute]
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    public bool TypeSpecified
    {
        get
        {
            return this._type != Com.Honeywell.Hxps.Sdk.Model.DocumentType.NULL;
        }
        set
        {
            if (!value)
            {
                this._type = Com.Honeywell.Hxps.Sdk.Model.DocumentType.NULL;
            }
        }
    }
    /// <summary>
    ///  (no documentation provided)
    /// </summary>
    [System.Xml.Serialization.XmlElementAttribute(ElementName = "description", Namespace = "")]
    [System.Xml.Serialization.SoapElementAttribute(ElementName = "description")]
    public string Description
    {
        get
        {
            return this._description;
        }
        set
        {
            this._description = value;
        }
    }
    /// <summary>
    ///  (no documentation provided)
    /// </summary>
    [System.Xml.Serialization.XmlElementAttribute(ElementName = "displayName", Namespace = "")]
    [System.Xml.Serialization.SoapElementAttribute(ElementName = "displayName")]
    public string DisplayName
    {
        get
        {
            return this._displayName;
        }
        set
        {
            this._displayName = value;
        }
    }
    /// <summary>
    ///  (no documentation provided)
    /// </summary>
    [System.Xml.Serialization.XmlElementAttribute(ElementName = "docId", Namespace = "")]
    [System.Xml.Serialization.SoapElementAttribute(ElementName = "docId")]
    public string DocId
    {
        get
        {
            return this._docId;
        }
        set
        {
            this._docId = value;
        }
    }
    /// <summary>
    ///  (no documentation provided)
    /// </summary>
    [System.Xml.Serialization.XmlElementAttribute(ElementName = "version", Namespace = "")]
    [System.Xml.Serialization.SoapElementAttribute(ElementName = "version")]
    public Com.Honeywell.Hxps.Sdk.Model.Impl.VersionImpl Version
    {
        get
        {
            return this._version;
        }
        set
        {
            this._version = value;
        }
    }
    /// <summary>
    ///  (no documentation provided)
    /// </summary>
    [System.Xml.Serialization.XmlElementAttribute(ElementName = "fields", Namespace = "")]
    [System.Xml.Serialization.SoapElementAttribute(ElementName = "fields")]
    public object Fields
    {
        get
        {
            return this._fields;
        }
        set
        {
            this._fields = value;
        }
    }
    /// <summary>
    ///  (no documentation provided)
    /// </summary>
    [System.Xml.Serialization.XmlElementAttribute(ElementName = "revision", Namespace = "")]
    [System.Xml.Serialization.SoapElementAttribute(ElementName = "revision")]
    public string Revision
    {
        get
        {
            return this._revision;
        }
        set
        {
            this._revision = value;
        }
    }
    /// <summary>
    ///  (no documentation provided)
    /// </summary>
    [System.Xml.Serialization.XmlElementAttribute(ElementName = "id", Namespace = "")]
    [System.Xml.Serialization.SoapElementAttribute(ElementName = "id")]
    public string Id
    {
        get
        {
            return this._id;
        }
        set
        {
            this._id = value;
        }
    }
}
}

In my main program, I get an array of xmlNodes when I try

Array fields = (Array)doc.Fields;

In the server side java REST service implementation, fields is actually a arraylist which will contain instances of three implementations of an interface. (List may contain ScalarFieldImpl or ArrayFieldImpl which are custom business objects).

I want to deserialize this xml fields into ScalarFieldImpl or ArrayFieldImpl using XmlSerializer. I want to know whether it is possible? If so, how do I do that?

A: 

As far as I know this isn't possible with the ootb xml (de-)serializer, I think you'll have to write your own XmlSerializer. Or try to use LINQ to XML.

JSC
Damn I wasn't awake yesterday, look at this link http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlarrayitemattribute.aspx
JSC
A: 

I got this working. I did few changes in my PSDocument class.

private object _fields; -> private ArrayList _fields;

Also I changed get/set method and added new metadata

[System.Xml.Serialization.XmlArray(ElementName = "fields", Namespace = "")] [System.Xml.Serialization.XmlArrayItem(ElementName="item")]

public ArrayList Fields { get { return this._fields; } set { this._fields = value; } }

Previously I had the following lines in my PSDocument class

[System.Xml.Serialization.XmlElementAttribute(ElementName = "fields", Namespace = "")] [System.Xml.Serialization.SoapElementAttribute(ElementName = "fields")] public object Fields { get { return this._fields; } set { this._fields = value; } }

So while deserializing fields in PSDocument, I get an arraylist with items as elements. Hope this helps someone.