我有一個XML文檔,看起來像這樣:反序列化XML文檔到.NET名單
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);
}
Person類:
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>[email protected]</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>[email protected]</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類。沒有鏈接到其他網站,但在你的問題的主體。 – ManoDestra
我有問題將XML粘貼到問題上。 IT DOSENT看起來不錯 –
只發布xml文本。不是視頻或圖片。 – jdweng