【发布时间】:2016-08-10 16:31:28
【问题描述】:
我有一个如下所示的 XML 文档:
https://gyazo.com/87aef26804136ee0cac49cf8b529f9cd
https://gyazo.com/aeb8c56689da52c67afe9b0bf7c19348
如何将其转换为 .NET List 对象?
我试过这个:
反序列化代码:
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (TextReader reader = new StringReader(xmlString))
{
List<Person> result = (List<Person>)serializer.Deserialize(reader);
}
人物类:
public class Person
{
public int id { get; set; }
public int client_id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public string phone_office { get; set; }
public string phone_mobile { get; set; }
public string fax { get; set; }
public string title { get; set; }
public DateTime createad_at { get; set; }
public DateTime updated_at { get; set; }
public bool isFromHighriseOrHarvest { get; set; }
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
-<people type="array">
-<person>
<author-id type="integer">543801</author-id>
<background>Vi är har jobbat ihop och är vänner / Nathalie</background>
<company-id type="integer">81499881</company-id>
<created-at type="datetime">2011-08-10T08:39:45Z</created-at>
<first-name>Per</first-name>
<group-id type="integer" nil="true"/>
<id type="integer">81500134</id>
<last-name>"Cromwell" (Eriksson)</last-name>
<owner-id type="integer" nil="true"/>
<title>ägare, grafiker</title>
<updated-at type="datetime">2011-08-16T08:17:43Z</updated-at>
<visible-to>Everyone</visible-to>
<company-name>Studio Total</company-name>
<linkedin-url nil="true"/>
< avatar_url>https://secure.highrisehq.com/avatar_proxy/eJxj4Yhmz2SWLWTMZHk2_TYLABiEBDM|9d29b49d8f165ff33f28b7f7fac2926eb8487319</avatar_url>
-<contact-data>
-<web-addresses type="array">
-<web-address>
<id type="integer">70306124</id>
<location>Work</location>
<url>http://www.studiototal.se</url>
</web-address>
</web-addresses>
<twitter-accounts type="array"/>
-<email-addresses type="array">
-<email-address>
<address>per@studiototal.se</address>
<id type="integer">39720318</id>
<location>Work</location>
</email-address>
</email-addresses>
<addresses type="array"/>
-<phone-numbers type="array">
-<phone-number>
<id type="integer">70306123</id>
<location>Work</location>
<number>0703689909</number>
</phone-number>
</phone-numbers>
<instant-messengers type="array"/>
</contact-data>
</person>
-<person>
<author-id type="integer">848257</author-id>
<background/>
<company-id type="integer">153838696</company-id>
<created-at type="datetime">2013-02-18T12:49:37Z</created-at>
<first-name>"Kristofer"</first-name>
<group-id type="integer" nil="true"/>
<id type="integer">153838730</id>
<last-name>"Malmer"</last-name>
<owner-id type="integer" nil="true"/>
<title>Projektledare Online listening</title>
<updated-at type="datetime">2013-02-18T12:49:37Z</updated-at>
<visible-to>Everyone</visible-to>
<company-name>Santa Maria</company-name>
<linkedin-url nil="true"/>
<avatar_url>https://secure.highrisehq.com/avatar_proxy/eJxj4Yhmz2SWLWTMZOlK0eYEABUgAvk|d7e22f72a1a3ae2efa83df54e4184d429120cd9f</avatar_url>
-<contact-data>
<web-addresses type="array"/>
<twitter-accounts type="array"/>
<email-addresses type="array"/>
<addresses type="array"/>
-<phone-numbers type="array">
-<phone-number>
<id type="integer">129346649</id>
<location>Work</location>
<number>031-674151</number>
</phone-number>
</phone-numbers>
<instant-messengers type="array"/>
</contact-data>
</person>
-<person>
<author-id type="integer">848257</author-id>
<background/>
<company-id type="integer">151848665</company-id>
<created-at type="datetime">2013-02-01T10:14:27Z</created-at>
<first-name>"Sorush"</first-name>
<group-id type="integer" nil="true"/>
<id type="integer">151848627</id>
<last-name/>
<owner-id type="integer" nil="true"/>
<title/>
<updated-at type="datetime">2013-02-01T10:16:29Z</updated-at>
<visible-to>Everyone</visible-to>
<company-name>Rancold</company-name>
<linkedin-url nil="true"/>
<avatar_url>https://secure.highrisehq.com/avatar_proxy/eJxj4Yhmz2SWLWTMZNnMxssJABRuAqY|1606d5054fb0e0f0b5ccc657dffcd80966ab9b64</avatar_url>
-<contact-data>
-<web-addresses type="array">
-<web-address>
<id type="integer">127911276</id>
<location>Work</location>
<url>http://www.rancold.com</url>
</web-address>
</web-addresses>
<twitter-accounts type="array"/>
-<email-addresses type="array">
-<email-address>
<address>sa@rancold.com</address>
<id type="integer">76736018</id>
<location>Work</location>
</email-address>
</email-addresses>
<addresses type="array"/>
-<phone-numbers type="array">
-<phone-number>
<id type="integer">127911275</id>
<location>Work</location>
<number>031-7441284</number>
</phone-number>
</phone-numbers>
<instant-messengers type="array"/>
</contact-data>
</person>
错误是System.InvalidOperationException.
Additional message: there is something wrong with your xml document (2, 2)。
【问题讨论】:
-
在此处包含一个 XML 文件的小样本(一个或两个节点就足够了)。另外,包括您的 Person 类。不是指向其他网站的链接,而是在您的问题正文中。
-
我在将 XML 粘贴到问题中时遇到问题。它看起来不错
-
只发布 xml 文本。不是视频或图片。
-
使用带箭头的图标 ,或在网站上发布 xml 而只是文本。
-
跳过一行,缩进四个空格,它会给你一个代码块。它应该看起来不错。请发布 full XML 以及异常的完整详细信息 - Visual Studio 应该为您提供将异常详细信息复制到剪贴板的选项。
标签: c# .net xml deserialization