1

私はC#の初心者です。xml 応答を返す Java REST サービスがあり、C# XmlSerializer を使用して xml ドキュメントを逆シリアル化しようとしています。サンプルの xml ドキュメント応答を以下に貼り付けます。

<?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>

この xml ドキュメントを Document オブジェクトに逆シリアル化すると、正常に動作します。私のドキュメントクラスは下に貼り付けられています

[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;
        }
    }
}
}

私のメイン プログラムでは、試してみると xmlNodes の配列を取得します。

Array fields = (Array)doc.Fields;

サーバー側の Java REST サービス実装では、fields は実際には、インターフェースの 3 つの実装のインスタンスを含む配列リストです。(リストには、カスタム ビジネス オブジェクトである ScalarFieldImpl または ArrayFieldImpl を含めることができます)。

XmlSerializer を使用して、この xml フィールドを ScalarFieldImpl または ArrayFieldImpl にデシリアライズしたいと考えています。可能か知りたいのですが?もしそうなら、どうすればいいですか?

4

2 に答える 2

0

私の知る限り、これは ootb xml (de-)serializer では不可能です。独自の XmlSerializer を作成する必要があると思います。または、LINQ to XML を使用してみてください。

于 2010-10-25T14:55:41.557 に答える
0

私はこれを機能させました。PSDocument クラスにいくつかの変更を加えました。

プライベート オブジェクト _fields; -> プライベート ArrayList _fields;

また、get/set メソッドを変更し、新しいメタデータを追加しました

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

public ArrayList フィールド { get { return this._fields; } セット { this._fields = 値; } }

以前は、PSDocument クラスに次の行がありました

[System.Xml.Serialization.XmlElementAttribute(ElementName = "fields", Namespace = "")] [System.Xml.Serialization.SoapElementAttribute(ElementName = "fields")] パブリック オブジェクト フィールド { get { return this._fields; } セット { this._fields = 値; } }

そのため、PSDocument のフィールドを逆シリアル化しているときに、要素としてアイテムを含む配列リストを取得します。これが誰かに役立つことを願っています。

于 2010-10-26T13:13:01.903 に答える