XML 序列化程序 - 反序列化 XML 数组
XML Serializer - Deserializing an XML array
我有以下示例 XML:
<PossibleAddresses>
<Address tempid="12345">1 The Street England</Address>
<Address tempid="6789">2 The Street England</Address>
<Address tempid="4321">3 The Street England</Address>
<Address tempid="1111">4 The Street England</Address>
</PossibleAddresses>
我正在尝试反序列化此 XML,以便我基本上只取回包含两个属性的 'Address' 对象的列表。一个是 'tempid' 另一个是实际地址字符串本身。
我的 class 看起来像这样:
[XmlRoot("PossibleAddresses")]
public class Addresses
{
[XmlArrayItem("Address", Type = typeof(Address))]
public List<Address> PossibleAddresses { get; set; }
}
public class Address
{
[XmlAttribute(AttributeName = "tempid")]
public string TempId { get; set; }
[XmlElement(ElementName = "Address")]
public string FullAddress { get; set; }
}
然后我使用 XmlSerializer 反序列化 XML。 T 是 class 'Addresses':
public static T DeserializeObject<T>(string data)
{
var ser = new XmlSerializer(typeof(T));
return (T)ser.Deserialize(new StringReader(data));
}
目前这将成功反序列化。我只是得到一个空的地址列表。
FullAddress
这里应该有属性 XmlText
[XmlText]
public string FullAddress { get; set; }
GoogleHireMe 是正确的,您需要 XmlText
属性。
public class Address
{
[XmlAttribute(AttributeName = "tempid")]
public string TempId { get; set; }
[XmlText]
public string FullAddress { get; set; }
}
但是,您可以将地址直接反序列化为数组:
var serializer =
new XmlSerializer(
typeof(Address[]),
new XmlRootAttribute("PossibleAddresses"));
Address[] items;
using(var stream = new StringReader(xml))
using(var reader = XmlReader.Create(stream))
{
items = (Address[]) serializer.Deserialize(reader);
}
我有以下示例 XML:
<PossibleAddresses>
<Address tempid="12345">1 The Street England</Address>
<Address tempid="6789">2 The Street England</Address>
<Address tempid="4321">3 The Street England</Address>
<Address tempid="1111">4 The Street England</Address>
</PossibleAddresses>
我正在尝试反序列化此 XML,以便我基本上只取回包含两个属性的 'Address' 对象的列表。一个是 'tempid' 另一个是实际地址字符串本身。
我的 class 看起来像这样:
[XmlRoot("PossibleAddresses")]
public class Addresses
{
[XmlArrayItem("Address", Type = typeof(Address))]
public List<Address> PossibleAddresses { get; set; }
}
public class Address
{
[XmlAttribute(AttributeName = "tempid")]
public string TempId { get; set; }
[XmlElement(ElementName = "Address")]
public string FullAddress { get; set; }
}
然后我使用 XmlSerializer 反序列化 XML。 T 是 class 'Addresses':
public static T DeserializeObject<T>(string data)
{
var ser = new XmlSerializer(typeof(T));
return (T)ser.Deserialize(new StringReader(data));
}
目前这将成功反序列化。我只是得到一个空的地址列表。
FullAddress
这里应该有属性 XmlText
[XmlText]
public string FullAddress { get; set; }
GoogleHireMe 是正确的,您需要 XmlText
属性。
public class Address
{
[XmlAttribute(AttributeName = "tempid")]
public string TempId { get; set; }
[XmlText]
public string FullAddress { get; set; }
}
但是,您可以将地址直接反序列化为数组:
var serializer =
new XmlSerializer(
typeof(Address[]),
new XmlRootAttribute("PossibleAddresses"));
Address[] items;
using(var stream = new StringReader(xml))
using(var reader = XmlReader.Create(stream))
{
items = (Address[]) serializer.Deserialize(reader);
}