2013-08-22 32 views
11

Tôi đang cố gắng để deserialize XML sau:C# Có một lỗi trong tài liệu XML (2, 2)

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

qua cuộc gọi này:

[...] 
    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); 
    } 
} 

để có được một thể hiện của lớp tương ứng:

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

Nhưng nó làm tăng lỗi There is an error in XML document (2, 2).
Tôi đã tìm một giải pháp cho điều này trong khoảng một giờ, nhưng nó không giúp được gì nhiều.

Tôi thậm chí đã cố gắng một thay đổi nhỏ mà không nên làm bất cứ điều gì:

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

Tuy nhiên, điều này không ngăn chặn các lỗi xảy ra. Nhưng vì nó chỉ đạt được để trả lại cho tôi một cá thể XMLGateResponse.XGResponse hoàn toàn trống rỗng (mọi phần tử/thuộc tính là null), nó không thực sự là một sự cải tiến.

Tôi biết loại câu hỏi này There is an error in XML document (2, 2) đã được thảo luận rất nhiều, nhưng tôi thực sự không tìm được giải pháp phù hợp với tôi.

+0

có ngoại lệ bên trong không? – Sayse

+0

@Sayse " không được mong đợi." – Serge

+2

Tôi tưởng tượng nó sẽ làm với 'AnonymousType = true, Namespace =" http://tempuri.org/XMLGateResponse "', nhưng tôi không quá chắc chắn xin lỗi .. – Sayse

Trả lời

10

XGResponse được trang trí với một XmlRootAttribute chỉ định tên không gian tên mặc định nhưng tài liệu của bạn thì không.

Hoặc loại bỏ khai gian tên này hoặc thêm xmlns="http://tempuri.org/XMLGateResponse" đến phần tử gốc của bạn xml

+0

Xe tăng hoạt động. Bạn có bất kỳ ý tưởng tại sao trang trí này được thiết lập bởi công cụ tiện ích XSD.exe? Bởi vì trước đây tôi chưa từng gặp vấn đề đó. – Serge

+1

Thực hiện theo mặc định để thực thi đặc tả của vùng tên XML trong tài liệu của bạn. Họ trông giống như một nỗi đau trong ass nhưng thực sự họ rất hữu ích cho phân vùng tài liệu và sử dụng chúng là một thực hành tuyệt vời để làm theo. Hãy xem xét nếu bạn lấy tài liệu của người khác và lồng vào bên trong tài liệu của riêng bạn. Cả hai tài liệu đều có các phần tử

nhưng các phần tử tiêu đề có dạng khác nhau. Nếu tài liệu lồng nhau có một vùng tên được chỉ định, chúng sẽ không có sự nhầm lẫn khi tìm kiếm theo tên phần tử. – Gusdor

+0

Ngoài ra, hãy xem xét XmlTypeAttribute như được ghi chú bởi @Sayse trong phần bình luận câu hỏi. Điều này có cùng chức năng khi kiểu không phải là phần tử gốc. – Gusdor

8

Nếu bạn cố gắng deserialise để loại sai bạn có thể nhận được các lỗi tương tự.
Ví dụ nếu bạn gọi

Deserialize<object>(myXml) 

hoặc

Deserialize<Failure>(myXml) 

Tôi biết đó là một thực tế xấu để trả lời câu hỏi khi 1) câu trả lời đã được cung cấp và 2) câu trả lời không chính xác là những gì người hỏi hỏi; nhưng tôi nghĩ rằng nó có thể tiết kiệm thời gian cho người khác tìm đường đến đây với một vấn đề không chính xác như người hỏi.

Các vấn đề liên quan