2015-12-03 13 views
15

を動作しません。私は、このXMLのためのデシリアライゼーションクラスを作成するために苦労しています:自動的に作成されたC#クラスが

<?xml version="1.0" encoding="ISO-8859-1"?> 
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:stn="urn:response"> 
    <SOAP-ENV:Body> 
     <Response> 
      <Records xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="stn:Record[1]"> 
       <item xsi:type="stn:Record"> 
        <person xsi:type="xsd:string">John Johnos</person> 
        <address xsi:type="xsd:string">Some Street 1</address> 
        <age xsi:type="xsd:string">24</age> 
       </item> 
      </Records> 
      <status xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="stn:status[1]"> 
       <item xsi:type="stn:status"> 
        <status xsi:type="xsd:string">success</status> 
        <message xsi:type="xsd:string"/> 
       </item> 
      </status> 
     </Response> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

私はVisualStudioを12に(自動的に作成されたコードを使用しようとしました:編集 - >貼り付け - クラスとして>貼り付けXML):

/// <remarks/> 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)] 
public partial class Envelope 
{ 

    private EnvelopeBody bodyField; 

    private string encodingStyleField; 

    /// <remarks/> 
    public EnvelopeBody Body 
    { 
     get 
     { 
      return this.bodyField; 
     } 
     set 
     { 
      this.bodyField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)] 
    public string encodingStyle 
    { 
     get 
     { 
      return this.encodingStyleField; 
     } 
     set 
     { 
      this.encodingStyleField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] 
public partial class EnvelopeBody 
{ 

    private Response responseField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "")] 
    public Response Response 
    { 
     get 
     { 
      return this.responseField; 
     } 
     set 
     { 
      this.responseField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class Response 
{ 

    private ResponseRecords recordsField; 

    private ResponseStatus statusField; 

    /// <remarks/> 
    public ResponseRecords Records 
    { 
     get 
     { 
      return this.recordsField; 
     } 
     set 
     { 
      this.recordsField = value; 
     } 
    } 

    /// <remarks/> 
    public ResponseStatus status 
    { 
     get 
     { 
      return this.statusField; 
     } 
     set 
     { 
      this.statusField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class ResponseRecords 
{ 

    private ResponseRecordsItem itemField; 

    private string arrayTypeField; 

    /// <remarks/> 
    public ResponseRecordsItem item 
    { 
     get 
     { 
      return this.itemField; 
     } 
     set 
     { 
      this.itemField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")] 
    public string arrayType 
    { 
     get 
     { 
      return this.arrayTypeField; 
     } 
     set 
     { 
      this.arrayTypeField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class ResponseRecordsItem 
{ 

    private string personField; 

    private string addressField; 

    private byte ageField; 

    /// <remarks/> 
    public string person 
    { 
     get 
     { 
      return this.personField; 
     } 
     set 
     { 
      this.personField = value; 
     } 
    } 

    /// <remarks/> 
    public string address 
    { 
     get 
     { 
      return this.addressField; 
     } 
     set 
     { 
      this.addressField = value; 
     } 
    } 

    /// <remarks/> 
    public byte age 
    { 
     get 
     { 
      return this.ageField; 
     } 
     set 
     { 
      this.ageField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class ResponseStatus 
{ 

    private ResponseStatusItem itemField; 

    private string arrayTypeField; 

    /// <remarks/> 
    public ResponseStatusItem item 
    { 
     get 
     { 
      return this.itemField; 
     } 
     set 
     { 
      this.itemField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")] 
    public string arrayType 
    { 
     get 
     { 
      return this.arrayTypeField; 
     } 
     set 
     { 
      this.arrayTypeField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class ResponseStatusItem 
{ 

    private string statusField; 

    private object messageField; 

    /// <remarks/> 
    public string status 
    { 
     get 
     { 
      return this.statusField; 
     } 
     set 
     { 
      this.statusField = value; 
     } 
    } 

    /// <remarks/> 
    public object message 
    { 
     get 
     { 
      return this.messageField; 
     } 
     set 
     { 
      this.messageField = value; 
     } 
    } 
} 

私はXmlSerializerをの助けを借りてデシリアライズしようとした:

var serializer = new XmlSerializer(typeof(Envelope)); 
var reader = new StringReader(response); 
var flResponse = (Envelope)serializer.Deserialize(reader); 
私が得た

エラーメッセージ:

Message=The specified type was not recognized: name='Array', namespace='http://schemas.xmlsoap.org/soap/encoding/', at <Records xmlns=''>. 

あなたはこのXMLのgreateのデシリアライゼーションのクラスに私を助けていただけますか?

+1

エンベロープクラスを表示できますか?または、この自動生成されたEnvelopeクラスは、XMLシリアライザで使用したクラスですか? – Angloos

+1

私はこの自動生成されたものを使用しました。 – belurd

+2

作成したXMLだけを修正したい場合は、[この例](http://siderite.blogspot.com/2014/01/deserializingserializing-xml-that.html)をご覧ください。xsi:type属性が奇妙な動作を引き起こすようです。 – DHP

答えて

5

私は事の多くを試み、finalyそれを考え出しました。 xsi:typeが逆シリアル化で機能しないため、あなたが投稿したXmlが無効です。

有効なXMLは次のようになります。

<?xml version="1.0" encoding="ISO-8859-1"?> 
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:stn="urn:response"> 
    <SOAP-ENV:Body> 
     <Response> 
      <Records> 
       <item> 
        <person >John Johnos</person> 
        <address >Some Street 1</address> 
        <age >24</age> 
       </item> 
      </Records> 
      <status> 
       <item> 
        <status >success</status> 
        <message/> 
       </item> 
      </status> 
     </Response> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

コードは次のようになります。

XDocument xml = XDocument.Parse(xmlInput); 

XmlSerializer serializer = new XmlSerializer(typeof(Response)); 

using (StringReader stream = new StringReader(items[0].ToString())) 
{ 
    var output = (Response)serializer.Deserialize(stream); 
} 

自動生成クラスから次のようになります。

<Response> 
    <Records xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <item> 
     <person>John Johnos</person> 
     <address >Some Street 1</address> 
     <age>24</age> 
    </item> 
    </Records> 
    <status xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <item > 
     <status >success</status> 
     <message /> 
    </item> 
    </status> 
</Response> 

これは十分に明らかであると思います。エンベロープから型を取り除く方法を知らないので、これはおそらくあなたが望む解決策ではありません。私は封筒から物事を取得するために使用

方法は、あなたがオブジェクトを埋めることができ、戻り配列またはその名前の要素のリストとXDocument.Descendants(elemmentName)です。そのより多くの仕事が、私はそのdeserializationのためのXMLを変換するよりも良いと思います。

+1

こんにちは。調査していただきありがとうございます。これは実際に私が最後にやったことです:xmlを通して検索し、すべての厄介な部分を削除し、後でクリーンなXMLをデシリアライズしてください。しかし、私はこれが生産システムにとって正しいとは思わない。これはちょうどstring.Replace()にはあまりにも汚いです。私が持っているXMLは私が制御できるものではありません。それは私が使用しなければならないサードパーティのサービスです。 – belurd

+1

同じ時刻に、このxsi:typeでデシリアライズすることは可能だと思いますが、実際には---( – belurd

+1

'XDocument.Descendants(" item ")のような' XDocument.Descendants(elemmentName) '文字列の配列を取得し、' item.Descendants( "person")。FirstOrDefault()。Value'を使用して 'John Johnos'を取得できます – Slasko

5

なぜスキーマ全体のシリアル化ライブラリを生成しないのですか?

  1. ダウンロードメッセージ内のURLからXSDスキーマファイルと、次のコマンドを

    XSD /クラスを

    http://schemas.xmlsoap.org/soap/encoding/

  2. 開きますどこかのVisual Studioコマンドプロンプトを、それを保存して入力してくださいSoapEncoding.xsd

  3. 出力はというファイルになります。

  4. このファイルをプロジェクトにインポートし、メッセージを再度デシリアライズしてみます。

すべてがうまくいけば、この時点ですべてがうまくいくはずです。

+1

私はこのようにしました。それは働かなかった。さらに、私の同僚の助けを求めると、一緒に間違っていることを見つけることができませんでした。 – belurd

+1

私が提案したアプローチはうまくいくはずです。私のアプローチを使ってどのようなエラーが発生しているかについていくつかの詳細を教えてください。 – Randy

関連する問題