简体   繁体   English

没有命名空间的 XmlSerializer 反序列化列表

[英]XmlSerializer Deserializing List Without namespace

Trying to deserialize List.试图反序列化列表。 Im getting the following error:我收到以下错误:

System.InvalidOperationException: There is an error in XML document (1, 2). System.InvalidOperationException: XML 文档 (1, 2) 中存在错误。 ---> System.InvalidOperationException: was not expected. ---> System.InvalidOperationException: 不是预期的。

Saw other quesitons like : {"<user xmlns=''> was not expected.} Deserializing Twitter XML but this does not solve my problem either.看到其他问题,如: {"<user xmlns=''> is not expected.} 反序列化 Twitter XML但这也不能解决我的问题。

This is an Xml Sample这是一个 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>

The class are created as follow:类创建如下:

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

Have tried changing the list to XmlArray, XmlArrayItem or Element.已尝试将列表更改为 XmlArray、XmlArrayItem 或 Element。 but still get the same error when I deserialize.但是当我反序列化时仍然出现相同的错误。

Deserializing Code Sample:反序列化代码示例:

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

This is ALL premised on the assumption that you have minimal control over the xml and don't have the luxury of changing that too much.这一切的前提是假设您对 xml 的控制权最小,并且没有太多改变的奢侈。 As others have noted, it is not well-formed.正如其他人所指出的那样,它的格式不是很好。 Here is one way to get serialization to work with minimal changes to your XML and types.这是使序列化在对 XML 和类型进行最小更改的情况下工作的一种方法。 First, get rid of your AuthorizationList type and assign an XmlType attribute to your Authorization type (this step serves to simply pluralize the name to match how your XML has it).首先,去掉您的AuthorizationList类型,并为您的AuthorizationList类型分配一个XmlType属性(此步骤用于简单地将名称复数化以匹配您的 XML 的方式)。

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

public class List { ... }

Wrap your XML in the following root element:将您的 XML 包装在以下根元素中:

<ArrayOfAuthorizations>
...
</ArrayOfAuthorizations>

The XML now represents a list of "authorizations" so to deserialize is just this: XML 现在表示“授权”列表,因此反序列化就是这样:

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

Another Solution:另一个解决方案:

Change the Authorizations member to be of type Authorization[] (array type rather than singular) and to have an XmlElement attribute (not XmlArray ).Authorizations成员更改为Authorizations Authorization[]类型(数组类型而不是单数)并具有XmlElement属性(不是XmlArray )。 Apply the XmlType attribute to the Authorization (as with the above solution this is to match the xml since it has the pluralized name for each array element).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; }
}

Like before, you need to wrap your XML with the matching 'AuthorizationList' root element:像以前一样,您需要使用匹配的“AuthorizationList”根元素来包装您的 XML:

<AuthorizationList>
...
</AuthorizationList>

Then you deserialize instance of your AuthorizationList type rather that List<T> as with the previous solution.然后你反序列化你的AuthorizationList类型的实例,而不是List<T>与以前的解决方案一样。

AuthorizationList l = FromXml<AuthorizationList>(xml);

Note that the root XML element will also need to match that type name also.请注意,根 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