2017-03-07 3 views
2

XMLへのサービス参照を使用してプロジェクトに追加されたオブジェクトのシリアル化に問題があります。C#Referencedオブジェクトのシリアライズ

サービスリファレンスを介して参照されるオブジェクトは、以下の構造を有する:

public class GetProspectsContactStatusParametersV1 
{ 
    public GetProspectsContactStatusParametersV1() 
    { 
     Version = 1; 
    } 

    public int Version { get; set; } 

    public ProspectIds ProspectIDs { get; set; } 

    public InterestIds InterestIDs { get; set; } 

    public class ProspectIds 
    { 
     private List<int> _prospectIds = new List<int>(); 
     [XmlElement("ProspectId")] 
     public List<int> Items 
     { 
      get { return _prospectIds; } 
      set { _prospectIds = value; } 
     } 
    } 

    public class InterestIds 
    { 
     private List<int> _interestIds = new List<int>(); 
     [XmlElement("InterestId")] 
     public List<int> Items 
     { 
      get { return _interestIds; } 
      set { _interestIds = value; } 
     } 
    } 
} 

このコードは、私が別のアプリケーションで参照するWebサービスプロジェクトの一部を形成します。次のように私は上記のオブジェクトのインスタンスを作成し、他のプロジェクトでの参照の使用:

var request = new ProspectsWebServiceMetadata.GetProspectsContactStatusParametersV1(); 
request.Version = 1; 
request.InterestIDs = new ProspectsWebServiceMetadata.InterestIds 
{ 
    2,3,4,5 
}; 

そして、私はその後、基本的にXmlSerializerを使用してXMLにこのオブジェクトをシリアル化しよう:

public static string ToXmlString(object source) 
{ 
    using (StringWriter sww = new StringWriter()) 
    using (XmlWriter writer = XmlWriter.Create(sww)) 
    { 
     System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(source.GetType()); 

     ser.Serialize(writer, source); 

     return sww.ToString(); 
    } 
} 

そのXMLは、

<?xml version="1.0" encoding="utf-16"?> 
    <GetProspectsContactStatusParametersV1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Version>1</Version> 
    <InterestIDs> 
     <int>2</int> 
     <int>3</int> 
     <int>4</int> 
     <int>5</int> 
    </InterestIDs> 

XML aの一部を次のように作成されますです"InterestIDs"内のbove には "int"ではなく "InterestID"が必要です。

参照のインポートの一部として生成されたコードは、それが正しくシリアル化する必要があることを示していると思われるが、明らかにそれはしていません:

[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")] 
[System.Runtime.Serialization.CollectionDataContractAttribute(Name="InterestIds", Namespace="http://www.testing.com/", ItemName="InterestId")] 
[System.SerializableAttribute()] 
public class InterestIds : System.Collections.Generic.List<int> { 
} 

任意のアイデア?

答えて

0

CollectionDataContractAttributeの最終スニペットの属性は、ではなく、DataContractSerializerのデータを実際にマークしています。

+0

あなたの提案から、http://stackoverflow.com/questions/7348240/xml-serialization-using-datacontractserializer-and-xmlserializer?rq=1という質問が見つかりました。 '[DataContract]'と '[DataMember]'を追加すると、シリアライゼーションは正しく動作します。 –

関連する問題