将 XML 值反序列化为对象
Deserialize XML values to objects
我正在尝试使用 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());
}
你可以在这里简单地使用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();
}
}
}
您可以将 XmlRoot
添加到 class 以声明该元素的命名空间:
[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 作为对象。
使用 the XSD tool 生成 class,然后使用 XmlSerializer
填充 class。像这样。
MyClass myClass;
using (var stream = new FileStream("myClass.xml", FileMode.Open))
{
var serializer = new XmlSerializer(typeof(MyClass));
myClass = serializer.Deserialize(stream);
}
我找到了一个网站,该网站将采用 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; }
}
我正在尝试使用 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());
}
你可以在这里简单地使用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();
}
}
}
您可以将 XmlRoot
添加到 class 以声明该元素的命名空间:
[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 作为对象。
使用 the XSD tool 生成 class,然后使用 XmlSerializer
填充 class。像这样。
MyClass myClass;
using (var stream = new FileStream("myClass.xml", FileMode.Open))
{
var serializer = new XmlSerializer(typeof(MyClass));
myClass = serializer.Deserialize(stream);
}
我找到了一个网站,该网站将采用 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; }
}