【问题标题】:Deserialize XML doc to .NET list将 XML 文档反序列化为 .NET 列表
【发布时间】: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


【解决方案1】:

您应该使用 XML 序列化属性注释您的 Person 类型及其成员。请参阅Controlling XML Serialization Using Attributes 了解更多信息。

【讨论】:

    【解决方案2】:

    您可以根据您的数据自动构建您的 C# 类代码文件。首先,清理您的 XML(&lt; avatar_url&gt; 上的额外空格,添加最后的结束标记,摆脱杂散的 - 字符...

    给定好的 XML,保存到一个文件,然后在 VS 命令提示符下执行以下命令:

    xsd.exe people.xml
    

    这会从 XML 创建一个 XSD 文件。然后你需要创建你的代码文件:

    xsd.exe /c people.xsd
    

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

    【讨论】:

    • >xsd.exe 命令“xsd.exe”无效。 > 这就是我得到的
    • 打开一个 Visual Studio 命令提示符(开始菜单并输入“提示符”,你应该会在那里找到它——它在不同的 Visual Studio 版本中被称为不同的东西。或者在你的硬盘驱动器中搜索 xsd.exe 以查看它在哪里,并使用完整路径调用它。不要使用 Visual Studio 中的命令窗口。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多