C# XMLSerialize 抽象继承类型
我在 C# 中进行 XML 序列化时遇到问题。
我的课程:
public abstract class AbstractFeldtyp
{
private string _name;
private string _beschreibung;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Beschreibung
{
get
{
return _beschreibung;
}
set
{
_beschreibung = value;
}
}
}
public class Feldtyp_A : AbstractFeldtyp {
public Feldtyp_A() {
Name = "My name is A";
Beschreibung = "I'm a A";
}
}
public class Feldtyp_B : AbstractFeldtyp {
public Feldtyp_B() {
Name = "My name is B";
Beschreibung = "I'm not a A!";
}
}
public class My_Root {
private AbstractFeldtyp[] felder;
public My_Root() {
Name = "My name is B";
Beschreibung = "I'm not a A!";
felder = new AbstractFeldtyp[] { new Feldtyp_A(), new Feldtyp_B()};
}
}
现在我想将“My_Root”序列化为 XML 文件。
public class Serializer
{
private XmlSerializer s;
private FileStream stream;
public Serializer()
{
/* This is not acceptable! I must be dynamically */
Type[] t = new Type[]
{
typeof(Feldtyp_A),
typeof(Feldtyp_B)
};
/* End */
s = new XmlSerializer(typeof(My_Root), t);
AbstractMaschinenTyp m = new MaschineTyp1();
this.SerializeObject(m);
}
public void SerializeObject(object obj)
{
stream = new FileStream(@"D:\File.xml", FileMode.Create);
if(obj != null)
s.Serialize(stream, obj);
stream.Close();
}
public object DeserializeObject()
{
stream = new FileStream(@"D:\File.xml", FileMode.Open);
object o = s.Deserialize(stream);
stream.Close();
return o;
}
当然,它有效。但我在编译时不知道有哪些类...
我想要一个如下所示的 XML 文件:
<?xml version="1.0"?>
<My_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Felder>
<Feld>
<Feldtyp type="Feldtyp_A" />
</Feld>
<Feld>
<Feldtyp type="Feldtyp_B" />
</Feld>
</Felder>
</My_Root>
或者
<?xml version="1.0"?>
<My_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Felder>
<Feldtyp type="Feldtyp_A" />
<Feldtyp type="Feldtyp_B" />
</Felder>
</My_Root>
我不需要“Feldtyp_AB”中的属性。类型就够了。感谢您的帮助:)
I have a problem with the XML serialization from C#.
My classes:
public abstract class AbstractFeldtyp
{
private string _name;
private string _beschreibung;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Beschreibung
{
get
{
return _beschreibung;
}
set
{
_beschreibung = value;
}
}
}
public class Feldtyp_A : AbstractFeldtyp {
public Feldtyp_A() {
Name = "My name is A";
Beschreibung = "I'm a A";
}
}
public class Feldtyp_B : AbstractFeldtyp {
public Feldtyp_B() {
Name = "My name is B";
Beschreibung = "I'm not a A!";
}
}
public class My_Root {
private AbstractFeldtyp[] felder;
public My_Root() {
Name = "My name is B";
Beschreibung = "I'm not a A!";
felder = new AbstractFeldtyp[] { new Feldtyp_A(), new Feldtyp_B()};
}
}
Now I want to serialize "My_Root" to a XML-File.
public class Serializer
{
private XmlSerializer s;
private FileStream stream;
public Serializer()
{
/* This is not acceptable! I must be dynamically */
Type[] t = new Type[]
{
typeof(Feldtyp_A),
typeof(Feldtyp_B)
};
/* End */
s = new XmlSerializer(typeof(My_Root), t);
AbstractMaschinenTyp m = new MaschineTyp1();
this.SerializeObject(m);
}
public void SerializeObject(object obj)
{
stream = new FileStream(@"D:\File.xml", FileMode.Create);
if(obj != null)
s.Serialize(stream, obj);
stream.Close();
}
public object DeserializeObject()
{
stream = new FileStream(@"D:\File.xml", FileMode.Open);
object o = s.Deserialize(stream);
stream.Close();
return o;
}
Sure, it works. But i dont know at compile-time what classes are there...
I want a XML-File that looks like:
<?xml version="1.0"?>
<My_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Felder>
<Feld>
<Feldtyp type="Feldtyp_A" />
</Feld>
<Feld>
<Feldtyp type="Feldtyp_B" />
</Feld>
</Felder>
</My_Root>
or
<?xml version="1.0"?>
<My_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Felder>
<Feldtyp type="Feldtyp_A" />
<Feldtyp type="Feldtyp_B" />
</Felder>
</My_Root>
I do not need the properties from "Feldtyp_AB". The the type is enough. Thanks for help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论