2016-08-10 23 views
0

我有一個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)

+3

包括您的XML文件的一個小樣本這裏(一個或兩個節點應該是足夠了)。此外,請包括您的Person類。沒有鏈接到其他網站,但在你的問題的主體。 – ManoDestra

+0

我有問題將XML粘貼到問題上。 IT DOSENT看起來不錯 –

+3

只發布xml文本。不是視頻或圖片。 – jdweng

回答

1

您可以根據您的數據自動構建您的C#類代碼文件。首先,清理你的XML(在< avatar_url>上添加空格,添加最後的結束標記,擺脫-字符的偏離...

給定好的XML,保存到文件,然後從VS命令提示符執行以下命令:

xsd.exe people.xml 

這個創建的XML XSD文件然後,你需要創建你的代碼文件:。

xsd.exe /c people.xsd 

現在你必須擁有一切正確配置一個C#的CodeFile您可以修改此的CodeFile爲了您的需要,但只保留所有屬性和其他與XML相關的內容。

+0

> xsd.exe 命令「xsd.exe」無效。 >這就是我得到的 –

+0

打開一個Visual Studio命令提示符(開始菜單並鍵入「提示符」,你應該在那裏找到它 - 在不同的Visual Studio版本中調用不同的東西,或者在硬盤上搜索xsd.exe以查看它在哪裏,並用完整路徑調用它。不要在Visual Studio中使用命令窗口。 –

0

嘗試XML LINQ

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication7 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 
      Person.people = doc.Descendants("person").Select(x => new Person() 
      { 
       id = (int)x.Element("author-id"), 
       client_id = (int)x.Element("company-id"), 
       first_name = (string)x.Element("first-name"), 
       last_name = (string)x.Element("last-name"), 
       email = (string)x.Descendants("email-addresses").FirstOrDefault(), 
       phone_office = x.Descendants("phone-number").Where(y => (string)y.Element("location") == "Work").Select(z => (string)x.Descendants("number").FirstOrDefault()).FirstOrDefault(), 
       phone_mobile = x.Descendants("phone-number").Where(y => (string)y.Element("location") == "Mobile").Select(z => (string)x.Descendants("number").FirstOrDefault()).FirstOrDefault(), 
       fax = x.Descendants("phone-number").Where(y => (string)y.Element("location") == "Fax").Select(z => (string)x.Descendants("number").FirstOrDefault()).FirstOrDefault(), 
       title = (string)x.Element("title"), 
       createad_at = (DateTime)x.Element("created-at"), 
       updated_at = (DateTime)x.Element("updated-at"), 

      }).ToList(); 

     } 
    } 
    public class Person 
    { 
     public static List<Person> people = new List<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; } 
    } 
}