C# 将XML文档反序列化为.NET列表

C# 将XML文档反序列化为.NET列表,c#,.net,xml,deserialization,C#,.net,Xml,Deserialization,我有一个XML文档,如下所示: 如何将其转换为.NET列表对象 我试过这个: 反序列化代码: XmlSerializer serializer = new XmlSerializer(typeof(Person)); using (TextReader reader = new StringReader(xmlString)) { List<Person> result = (List<Person>)serializer.Deserialize(reader

我有一个XML文档,如下所示:

如何将其转换为.NET列表对象

我试过这个:

反序列化代码:

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 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>
XML:

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

-
-
543801
Viär har jobbat ihop ochär vänner/Nathalie
81499881
2011-08-10T08:39:45Z
每
81500134
“克伦威尔”(埃里克森)
格拉菲克
2011-08-16T08:17:43Z
每个人
演播室总数
https://secure.highrisehq.com/avatar_proxy/eJxj4Yhmz2SWLWTMZHk2_TYLABiEBDM|9D29B49D8F165FF33F28B7F7FAC296EB8487319
-
-
-
70306124
工作
http://www.studiototal.se
-
-
per@studiototal.se
39720318
工作
-
-
70306123
工作
0703689909
-
848257
153838696
2013-02-18T12:49:37Z
“克里斯托弗”
153838730
“马尔默”
项目在线收听
2013-02-18T12:49:37Z
每个人
圣玛丽亚
https://secure.highrisehq.com/avatar_proxy/eJxj4Yhmz2SWLWTMZOlK0eYEABUgAvk|d7e22f72a1a3ae2efa83df54e4184d429120cd9f
-
-
-
129346649
工作
031-674151
-
848257
151848665
2013-02-01T10:14:27Z
“索鲁什”
151848627
2013-02-01T10:16:29Z
每个人
积怨
https://secure.highrisehq.com/avatar_proxy/eJxj4Yhmz2SWLWTMZNnMxssJABRuAqY|1606d5054fb0e0f0b5ccc657dffcd80966ab9b64
-
-
-
127911276
工作
http://www.rancold.com
-
-
sa@rancold.com
76736018
工作
-
-
127911275
工作
031-7441284
错误为
System.InvalidOperationException。

附加消息:您的xml文档(2,2)有问题。

您应该使用xml序列化属性注释您的个人类型及其成员。有关详细信息,请参阅。

您应该使用XML序列化属性为您的个人类型及其成员添加注释。有关更多信息,请参阅。

您可以根据数据自动构建C#class代码文件。首先,清理您的XML(在
上有额外的空白,添加最后的结束标记,除去散乱的
-
字符

给定良好的XML,保存到文件,并在VS命令提示符下执行以下操作:

xsd.exe people.xml
这将从XML创建一个XSD文件。然后您需要创建代码文件:

xsd.exe /c people.xsd
现在,您有了一个C#代码文件,该文件已正确配置了所有内容。您可以根据需要修改此代码文件,但不要使用所有属性和其他与XML相关的内容。

您可以根据数据自动构建C#类代码文件。首先,清理XML(在
上添加了额外的空白,添加了最后的结束标记,去掉了散乱的
-
字符

给定良好的XML,保存到文件,并在VS命令提示符下执行以下操作:

xsd.exe people.xml
这将从XML创建一个XSD文件。然后您需要创建代码文件:

xsd.exe /c people.xsd
现在您有了一个C#代码文件,它已正确配置了所有内容。您可以根据需要修改此代码文件,但不要使用所有属性和其他与XML相关的内容。

试试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; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序7
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
Person.people=文档后代(“Person”)。选择(x=>newperson()
{
id=(int)x.Element(“作者id”),
客户id=(int)x.Element(“公司id”),
first_name=(string)x.Element(“first name”),
姓氏=(字符串)x.Element(“姓氏”),
email=(字符串)x.x(“电子邮件地址”).FirstOrDefault(),
phone_office=x.subjections(“电话号码”)。其中(y=>(字符串)y.Element(“位置”)==“工作”)。选择(z=>(字符串)x.subjections(“号码”).FirstOrDefault()).FirstOrDefault(),
phone_mobile=x.substands(“电话号码”)。其中(y=>(字符串)y.Element(“位置”)==“手机”)。选择(z=>(字符串)x.substands(“号码”).FirstOrDefault()).FirstOrDefault(),
fax=x.subjections(“电话号码”)。其中(y=>(字符串)y.Element(“位置”)==“传真”)。选择(z=>(字符串)x.subjections(“号码”).FirstOrDefault()).FirstOrDefault(),
title=(字符串)x.Element(“title”),
createad_at=(DateTime)x.Element(“创建于”),
更新时间=(日期时间)x.Element(“更新时间”),
}).ToList();
}
}
公共阶层人士
{
公共静态列表人员=新列表();
公共int id{get;set;}
public int client_id{get;set;}
公共字符串first_name{get;set;}
公共字符串last_name{get;set;}
公共字符串电子邮件{get;set;}
公用字符串电话局{get;set;}
公共字符串电话_mobile{get;set;}
公共字符串传真{get;set;}
公共字符串标题{get;set;}
在{get;set;}处创建的公共日期时间
公共日期时间在{get;set;}更新
公共布尔值来自HighriseOrHarvest{get;set;}
}
}
试试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; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序7
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
Person.people=文档后代(“Person”)。选择(x=>newperson()
{
id=(int)x.Element(“作者id”),
客户id=(int)x.Element(“公司id”),
first_name=(string)x.Element(“first name”),
姓氏=(字符串)