将XML值反序列化为对象

时间:2019-07-15 22:04:03

标签: c# xml-deserialization

我正在尝试使用XDocument读取XML并将其反序列化为对象。

我要使用以下XML:

<r25:spaces xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xl="http://www.w3.org/1999/xlink" xmlns:r25="http://www.collegenet.com/r25" pubdate="2019-07-15T14:51:16-07:00" engine="accl">
  <r25:space crc="00000022" status="est" xl:href="space.xml?space_id=200">
    <r25:space_id>200</r25:space_id>
    <r25:space_name>LRN 0001</r25:space_name>
    <r25:formal_name>Learning Commons -Test Scheduling Room</r25:formal_name>
    <r25:partition_id xl:href="rmpart.xml?partition_id=2">2</r25:partition_id>
    <r25:last_mod_dt>2019-07-11T08:01:00-07:00</r25:last_mod_dt>
  </r25:space>
</r25:spaces>

并将其反序列化为空格列表(其中Space具有以下定义):

public class Space
{
    public long space_id { get; set; }
    public string space_name { get; set; }
    public string formal_name { get; set; }
    public long partition_id { get ; set; }
    public DateTime last_mod_dt { get; set; }
}

我只得到了XElement。它死在串行器上

var doc = XDocument.Parse(result.Content);
XNamespace ns = "http://www.collegenet.com/r25";
XElement el = doc.Element(ns + "spaces");

foreach (var space in el.Elements())
{
    var serializer = new XmlSerializer(typeof(Space));
    var s = (Space)serializer.Deserialize(space.CreateReader());
}

4 个答案:

答案 0 :(得分:3)

您可以在这里简单地使用LINQ to XML。例如

using System;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApp22
{

    public class Space
    {
        public long space_id { get; set; }
        public string space_name { get; set; }
        public string formal_name { get; set; }
        public long partition_id { get; set; }
        public DateTime last_mod { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var xml = @"
<r25:spaces xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xl=""http://www.w3.org/1999/xlink"" xmlns:r25=""http://www.collegenet.com/r25"" pubdate=""2019-07-15T14:51:16-07:00"" engine=""accl"">
  <r25:space crc=""00000022"" status=""est"" xl:href=""space.xml?space_id=200"">
    <r25:space_id>200</r25:space_id>
    <r25:space_name>LRN 0001</r25:space_name>
    <r25:formal_name>Learning Commons -Test Scheduling Room</r25:formal_name>
    <r25:partition_id xl:href=""rmpart.xml?partition_id=2"">2</r25:partition_id>
    <r25:last_mod_dt>2019-07-11T08:01:00-07:00</r25:last_mod_dt>
  </r25:space>
</r25:spaces>
";
            var doc = XDocument.Parse(xml);
            XNamespace ns = "http://www.collegenet.com/r25";

            var q = from e in doc.Element(ns + "spaces").Elements()
                    select new Space
                    {
                        space_id = (int)e.Element(ns + "space_id"),
                        space_name = (string)e.Element(ns + "space_name"),
                        formal_name = (string)e.Element(ns + "formal_name"),
                        partition_id = (long)e.Element(ns + "partition_id"),
                        last_mod = (DateTime)e.Element(ns + "last_mod_dt")
                    };

            var space = q.First();
        }
    }
}

答案 1 :(得分:1)

您可以将XmlRoot添加到类中以声明该元素的名称空间:

[XmlRoot("space", Namespace = "http://www.collegenet.com/r25")]
public class Space
{
    public long space_id { get; set; }
    public string space_name { get; set; }
    public string formal_name { get; set; }
    public long partition_id { get; set; }
    public DateTime last_mod { get; set; }
}

反序列化程序现在将正确读取XML作为对象。

答案 2 :(得分:0)

使用the XSD tool生成类,然后使用XmlSerializer填充类。这样。

MyClass myClass;
using (var stream = new FileStream("myClass.xml", FileMode.Open))
{
    var serializer = new XmlSerializer(typeof(MyClass));
    myClass = serializer.Deserialize(stream);
}

答案 3 :(得分:0)

我找到了一个使用XML并创建适当的反序列化类的网站。 Xml2CSharp,创建了以下允许反序列化的类:

[XmlRoot(ElementName="partition_id", Namespace="http://www.collegenet.com/r25")]
public class Partition_id {
    [XmlAttribute(AttributeName="href", Namespace="http://www.w3.org/1999/xlink")]
    public string Href { get; set; }
    [XmlText]
    public string Text { get; set; }
}

[XmlRoot(ElementName="space", Namespace="http://www.collegenet.com/r25")]
public class Space {
    [XmlElement(ElementName="space_id", Namespace="http://www.collegenet.com/r25")]
    public string Space_id { get; set; }
    [XmlElement(ElementName="space_name", Namespace="http://www.collegenet.com/r25")]
    public string Space_name { get; set; }
    [XmlElement(ElementName="formal_name", Namespace="http://www.collegenet.com/r25")]
    public string Formal_name { get; set; }
    [XmlElement(ElementName="partition_id", Namespace="http://www.collegenet.com/r25")]
    public Partition_id Partition_id { get; set; }
    [XmlElement(ElementName="last_mod_dt", Namespace="http://www.collegenet.com/r25")]
    public string Last_mod_dt { get; set; }
    [XmlAttribute(AttributeName="crc")]
    public string Crc { get; set; }
    [XmlAttribute(AttributeName="status")]
    public string Status { get; set; }
    [XmlAttribute(AttributeName="href", Namespace="http://www.w3.org/1999/xlink")]
    public string Href { get; set; }
}

[XmlRoot(ElementName="spaces", Namespace="http://www.collegenet.com/r25")]
public class Spaces {
    [XmlElement(ElementName="space", Namespace="http://www.collegenet.com/r25")]
    public Space Space { get; set; }
    [XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
    public string Xsi { get; set; }
    [XmlAttribute(AttributeName="xl", Namespace="http://www.w3.org/2000/xmlns/")]
    public string Xl { get; set; }
    [XmlAttribute(AttributeName="r25", Namespace="http://www.w3.org/2000/xmlns/")]
    public string R25 { get; set; }
    [XmlAttribute(AttributeName="pubdate")]
    public string Pubdate { get; set; }
    [XmlAttribute(AttributeName="engine")]
    public string Engine { get; set; }
}