[英]Correct XML representation of derived class
因此,我有以下基類和派生類:
[XmlInclude(typeof(Circle))]
[XmlInclude(typeof(Square))]
public class Shape
{
public int XPos { get; set; }
public int YPos { get; set; }
}
public class Circle : Shape
{
public int Radius { get; set; }
}
public class Square : Shape
{
public int Side { get; set; }
}
如何在XML中表示圓形和正方形,以便可以反序列化為新對象?
我正在努力:
<Shape type="Circle">
<Radius>10</Radius>
</Shape>
<Shape type="Square">
<Side>20</Side>
</Shape>
和
<Circle>
<Radius>10</Radius>
</Circle>
<Square>
<Side>20</Side>
</Square>
但它們都沒有使用當前派生類(Square或Circle)加載Shape對象。 第一個選項創建一個Square對象。 第二個根本不創建任何對象。 我正在使用C#MVC4。
[編輯-測試代碼]
using System;
using System.Xml.Serialization;
using System.IO;
public class Program
{
public static void Main()
{
var shape = new Shape { XPos = 4, YPos = 5 };
Console.WriteLine(Serialize(shape));
Console.WriteLine();
var circle = new Circle { XPos = 1, YPos = 2, Radius = 3 };
Console.WriteLine(Serialize(circle));
Console.WriteLine();
var square = new Square { XPos = 2, YPos = 1, Side = 5 };
var ser = Serialize(square);
Console.WriteLine(square);
Square sq = Deserialize(ser);
Console.WriteLine(sq.Side);
}
public static string Serialize(Shape objectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Shape));
StringWriter textWriter = new StringWriter();
xmlSerializer.Serialize(textWriter, objectToSerialize);
return textWriter.ToString();
}
public static Square Deserialize(string ser)
{
XmlSerializer serializer = new XmlSerializer(typeof(Square));
using (StringReader reader = new StringReader(ser))
{
return (Square)(serializer.Deserialize(reader));
}
}
[XmlInclude(typeof(Circle))]
[XmlInclude(typeof(Square))]
public class Shape
{
public int XPos { get; set; }
public int YPos { get; set; }
}
public class Circle : Shape
{
public int Radius { get; set; }
}
public class Square : Shape
{
public int Side { get; set; }
}
}
您將使用xsi:type
聲明Type
,例如xsi:type="Circle"
例
<Shape xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="Circle">
<XPos>1</XPos>
<YPos>2</YPos>
<Radius>3</Radius>
</Shape>
我的反序列化測試代碼
var shape = new Circle { XPos = 1, YPos = 2, Radius = 3};
var ser = new XmlSerializer(typeof(Shape));
var xml = string.Empty;
using(var sw = new StringWriter())
{
ser.Serialize(sw, shape);
xml = sw.ToString();
}
using (var sr = new StringReader(xml))
{
var obj = ser.Deserialize(sr);
}
如果您需要一種簡單的方法來返回強類型,則可以添加一個通用方法。
public T DeserializeShape<T>(string xml) where T : Shape
{
var ser = new XmlSerializer(typeof(Shape));
using (var sr = new StringReader(xml))
{
return (T)ser.Deserialize(sr);
}
}
var circle = DeserializeShape<Circle>(xml);
var square = DeserializeShape<Square>(xml); // etc....
下面的兩種方法將對形狀進行序列化和反序列化。 反序列化方法是通用的,以允許對不同形狀進行反序列化。
我提供了每個序列化為XML的對象的示例。 這是用於創建示例的點網小提琴 。 它還顯示了如何反序列化對象。
連載
public static string Serialize(Shape objectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Shape));
StringWriter textWriter = new StringWriter();
xmlSerializer.Serialize(textWriter, objectToSerialize);
return textWriter.ToString();
}
反序列化
public static T Deserialize<T>(string xmlToDeserialize) where T : Shape
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Shape));
using (StringReader reader = new StringReader(xmlToDeserialize)) {
return (T)xmlSerializer.Deserialize(reader);
}
}
示例形狀XML
<?xml version="1.0" encoding="utf-16"?>
<Shape xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<XPos>4</XPos>
<YPos>5</YPos>
</Shape>
圓形XML示例
<?xml version="1.0" encoding="utf-16"?>
<Shape xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Circle">
<XPos>1</XPos>
<YPos>2</YPos>
<Radius>3</Radius>
</Shape>
示例Square XML
<?xml version="1.0" encoding="utf-16"?>
<Shape xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Square">
<XPos>2</XPos>
<YPos>1</YPos>
<Side>5</Side>
</Shape>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.