2013-08-22 4 views
11

私は、次のXMLシリアル化を解除しようとしている:この呼び出しを通じてC#の(2、2)

<?xml version="1.0" encoding="UTF-8"?> 
<XGResponse><Failure code="400"> 
    Message id &apos;1&apos; was already submitted. 
</Failure></XGResponse> 

を:

[...] 
    var x = SerializationHelper.Deserialize<XMLGateResponse.XGResponse>(nResp); 
[...]   

public static T Deserialize<T>(string xml) 
{ 
    using (var str = new StringReader(xml)) 
    { 
     var xmlSerializer = new XmlSerializer(typeof(T)); 
     return (T)xmlSerializer.Deserialize(str); 
    } 
} 

のインスタンスを取得します対応するクラス:

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.18052 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

using System.Xml.Serialization; 

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1. 
// 

namespace XMLGateResponse 
{ 

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/XMLGateResponse")] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/XMLGateResponse", IsNullable = false)] 
    public partial class XGResponse 
    { 

     private object[] itemsField; 

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

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/XMLGateResponse")] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/XMLGateResponse", IsNullable = false)] 
    public partial class Failure 
    { 

     private string codeField; 

     private string titleField; 

     private string valueField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlAttributeAttribute(DataType = "NMTOKEN")] 
     public string code 
     { 
      get 
      { 
       return this.codeField; 
      } 
      set 
      { 
       this.codeField = value; 
      } 
     } 

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

     /// <remarks/> 
     [System.Xml.Serialization.XmlTextAttribute()] 
     public string Value 
     { 
      get 
      { 
       return this.valueField; 
      } 
      set 
      { 
       this.valueField = value; 
      } 
     } 
    } 

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/XMLGateResponse")] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/XMLGateResponse", IsNullable = false)] 
    public partial class Success 
    { 

     private string titleField; 

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

しかし、エラーThere is an error in XML document (2, 2)が発生します。
私は約1時間この解決策を探しましたが、あまり役に立たなかった。これは、エラーが起こることを防ぐん、しかし

public static T Deserialize<T>(string xml) 
{ 
    [...] 
     var xmlSerializer = new XmlSerializer(typeof(T), new XmlRootAttribute(typeof(T).Name)); 
    [...] 
} 

は、私も何もしてはならないわずかな変化を試してみました。しかし、それはXMLGateResponse.XGResponseインスタンスを完全に空にするだけです(すべての要素/属性がnullです)。これは改善ではありません。

私はこの種の質問をよく知っています。There is an error in XML document (2, 2)は既に多くのことが議論されていましたが、実際には私のために働く解決策は見つかりませんでした。

+0

内部例外がありますか? – Sayse

+0

@Sayse "は期待されていませんでした。 – Serge

+2

私はそれが 'AnonymousType = true、Namespace =" http://tempuri.org/XMLGateResponse "とは想像していましたが、あまりにも申し訳ありません。 – Sayse

答えて

10

XGResponseはデフォルトの名前空間名を指定するXmlRootAttributeで修飾されていますが、ドキュメントにはありません。

この名前空間宣言を削除するか、またはあなたが同じエラーを取得する可能性があります間違った型にdeserialiseしようとすると、XML

+0

タンクが動作します。このデコレータがユーティリティツールXSD.exeによって設定された理由はありますか?なぜなら私は以前にその問題を抱えていなかったからです。 – Serge

+1

デフォルトでは、ドキュメント内にXML名前空間の指定を強制します。彼らはお尻の痛みのように見えますが、実際にはドキュメントを分割するのに非常に便利で、それらを使用することは従うのが大変です。あなたが他の人の文書を取ってきて自分の文書の中に入れたらどうなるか考えてみてください。両方の文書には

の要素がありますが、ヘッダー要素の形式は異なります。ネストされたドキュメントに名前空間が指定されていれば、要素名で検索すると混乱することはありません。 – Gusdor

+0

また、質問コメントの@Sayseに記載されているXmlTypeAttributeについて考えてみましょう。これは、型がルート要素でない場合と同じ機能を持ちます。 – Gusdor

8

のルート要素にxmlns="http://tempuri.org/XMLGateResponse"を追加してください。たとえば
あなたは、私は1)の答えがすでに提供されてQと2に答えるために悪い習慣を知っている

Deserialize<object>(myXml) 

または

Deserialize<Failure>(myXml) 

を呼び出す場合)答え質問者が求めたものではない。質問者とまったく同じではない問題で、他の誰かが自分の道を探している時間を節約するかもしれないと思います。