簡體   English   中英

沒有命名空間的 XmlSerializer 反序列化列表

[英]XmlSerializer Deserializing List Without namespace

試圖反序列化列表。 我收到以下錯誤:

System.InvalidOperationException: XML 文檔 (1, 2) 中存在錯誤。 ---> System.InvalidOperationException: 不是預期的。

看到其他問題,如: {"<user xmlns=''> is not expected.} 反序列化 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 或 Element。 但是當我反序列化時仍然出現相同的錯誤。

反序列化代碼示例:

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

這一切的前提是假設您對 xml 的控制權最小,並且沒有太多改變的奢侈。 正如其他人所指出的那樣,它的格式不是很好。 這是使序列化在對 XML 和類型進行最小更改的情況下工作的一種方法。 首先,去掉您的AuthorizationList類型,並為您的AuthorizationList類型分配一個XmlType屬性(此步驟用於簡單地將名稱復數化以匹配您的 XML 的方式)。

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

public class List { ... }

將您的 XML 包裝在以下根元素中:

<ArrayOfAuthorizations>
...
</ArrayOfAuthorizations>

XML 現在表示“授權”列表,因此反序列化就是這樣:

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

另一個解決方案:

Authorizations成員更改為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”根元素來包裝您的 XML:

<AuthorizationList>
...
</AuthorizationList>

然后你反序列化你的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM