2016-08-05 57 views
0

試圖反序列化List。 即時得到以下錯誤:XmlSerializer非名稱空間反序列化列表

System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: was not expected.

看到別的quesitons,如:{"<user xmlns=''> was not expected.} Deserializing Twitter XML但這並不無論是解決我的問題。

這是一個XML示例

<authorizations> 
    <id>111</id> 
    <name>Name 1</name> 
    <Lists> 
    <List> 
     <id>1</id> 
     <id>2</id> 
    </List> 
    </Lists> 
</authorizations> 
<authorizations> 
    <id>222</id> 
    <name>Name 2</name> 
    <List /> 
</authorizations> 
<authorizations> 
    <id>333</id> 
    <name>Name 3</name> 
    <List /> 
</authorizations> 

類是如下創建:

public class Authorization 
    { 
     [XmlElement("id")] 
     public string Id{ get; set; } 
     [XmlElement("name")] 
     public string Name{ get; set; } 
     [XmlArray("Lists")] 
     [XmlArrayItem("List")] 
     public List[] Items{ get; set; } 
    } 

    public class List 
    { 
     [XmlElement("id")] 
     public string Id{ get; set; } 
    } 

    public class AuthorizationList 
    { 
     [XmlArray("authorizations")] 
     public Authorization Authorizations{ get; set; } 
    } 

試圖改變列表中XmlArray,XmlArrayItem或元素。但是當我反序列化時仍然會出現相同的錯誤。

反序列化代碼示例:

public static T FromXml<T>(string xmlString) 
    { 
     T obj = default(T); 

     if (!string.IsNullOrWhiteSpace(xmlString)) 
     { 
      using (var stringReader = new StringReader(xmlString)) 
      { 
       var xmlSerializer = new XmlSerializer(typeof(T)); 

       obj = (T)xmlSerializer.Deserialize(stringReader); 
      } 
     } 

     return obj; 
    } 
+0

這不是一個*有效的*(期望通過'XmlSerializer')xml。有效的從'<?xml version =「1.0」?>'開始。您可以嘗試在調用'Deserialize()'之前添加此行。另一件事是丟失數組項的容器(用'XmlRootAttribute'表示),請參閱[本](http://stackoverflow.com/q/126155/1997232)。 – Sinatr

+0

'XmlSerializer'可以處理缺少XML聲明的XML。但是,XML必須有且僅有一個[根元素](https://en.wikipedia.org/wiki/Root_element)。這個XML有多個根元素,所以根本就不是XML。 – dbc

+0

要嘗試添加根元素並讓你知道。感謝您的快速響應 – RSmart

回答

0

這一切前提是你必須在XML最低程度的控制,並且沒有改變太多的奢侈的假設。正如其他人所指出的那樣,它的形成並不完善。以下是通過對XML和類型進行最小更改來獲得序列化的一種方法。首先擺脫AuthorizationList類型,併爲您的授權類型分配一個XmlType屬性(此步驟簡單複用名稱以匹配xml的具體名稱)。

[XmlType("authorizations")] 
public class Authorization { ... } 

public class List { ... } 

裹在下面的根元素你的XML ...

<ArrayOfAuthorizations> 
... 
</ArrayOfAuthorizations> 

的XML現在代表 「授權」,使反序列化的名單只是這...

List<Authorization> l = FromXml<List<Authorization>>(xml); 

另一種解決方案...

更改Authorizations成員類型爲Authorization[](數組類型而不是單數),並具有XmlElement屬性(不是XmlArray)。將XmlType屬性應用於Authorization(與上述解決方案一樣,這是爲了匹配xml,因爲它具有每個數組元素的複數名稱)。

[XmlType("authorizations")] 
public class Authorization 
{ 
    [XmlElement("id")] 
    public string Id { get; set; } 
    [XmlElement("name")] 
    public string Name { get; set; } 
    [XmlArray("Lists")] 
    [XmlArrayItem("List")] 
    public List[] Items { get; set; } 
} 

public class List 
{ 
    [XmlElement("id")] 
    public string Id { get; set; } 
} 

public class AuthorizationList 
{ 
    [XmlElement("authorizations")] 
    public Authorization[] Authorizations { get; set; } 
} 

然後你反序列化AuthorizationList類型,而該List<T>的情況下,與以前的解決方案。

AuthorizationList l = FromXml<AuthorizationList>(xml); 

請注意,根XML元素也需要與該類型名稱匹配。

<AuthorizationList> 
<authorizations> 
    <id>111</id> 
    <name>Name 1</name> 
    <Lists> 
    <List> 
     <id>1</id> 
     <id>2</id> 
    </List> 
    </Lists> 
</authorizations> 
<authorizations> 
    <id>222</id> 
    <name>Name 2</name> 
    <List /> 
</authorizations> 
<authorizations> 
    <id>333</id> 
    <name>Name 3</name> 
    <List /> 
</authorizations> 
</AuthorizationList> 
+0

給了我同樣的錯誤,並不明白爲什麼要包裝根元素,如果它沒有在代碼中使用。 – RSmart

+0

我只是展示了一種方法,它明確表示您不需要定義純粹爲了滿足序列化程序的「AuthorizationList」類型。 'ArrayOf ...'根元素是'XmlSerializer'預期的默認約定,其中'...'是泛型類型參數的名稱。同樣,這是假設你想簡化和利用'List '而不是必須定義'AuthorizationList'來滿足根元素的要求。我會編輯並添加一個可以保留你的'AuthorizationList'的替代方法,因爲也許你需要這樣嗎? – blins

+0

查看我的編輯,它顯示了一個替代方案,它保持您的'AuthorizationList'類型並使其成爲xml中的根。 – blins

相關問題