我想使用XMLSerializer生成XML。我有一个抽象的Base类,它被其他类继承。
public abstract class Base
{
public string Name {get; set;}
public int ID {get; set;}
public Base(string Name, int ID)
{
this.Name = Name;
this.ID = ID;
}
}
public class HR: Base
{
public HR(string Name, int ID): Base(Name,ID)
{
}
}
public class IT : Base
{
public IT(string Name, int ID): Base(Name,ID)
{
}
}
我不确定如何生成格式为XML的文件
<Employee>
<HR>
<Name> </Name>
<ID> </ID>
</HR>
<IT>
<Name> </Name>
<ID> </ID>
</IT>
</Employee>
我为这个模糊的问题道歉。我之前从未使用过XMLSerializer,也不确定如何继续使用它。任何帮助将不胜感激。
由于
答案 0 :(得分:1)
将[Serializable] Annotation添加到要序列化的类中。
[System.Serializable]
public class Base
{
public string Name { get; set; }
public int ID { get; set; }
public Base(string Name, int ID)
{
this.Name = Name;
this.ID = ID;
}
}
要以XML格式序列化,请使用以下代码:
System.Xml.Serialization.XmlSerializer Serializer = new System.Xml.Serialization.XmlSerializer(typeof(Base));
Base Foo = new Base();
string xmldata = "";
using (var stringwriter = new System.IO.StringWriter())
{
using (System.Xml.XmlWriter xmlwriter = System.Xml.XmlWriter.Create(stringwriter))
{
Serializer.Serialize(xmlwriter, Foo);
xml = stringwriter.ToString(); // Your XML
}
}
要从XML反序列化回Base对象,请使用以下代码:
System.IO.MemoryStream FooStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml));
Base Foo;
Foo = (Base)Serializer.Deserialize(FooStream);
答案 1 :(得分:1)
我认为您需要使用XmlType
属性来确保您的元素显示为<HR>
和<IT>
而不是<employee xsi:type="HR">
。下面的工作演示:
public abstract class Employee
{
public string Name { get; set; }
public string ID { get; set; }
public Employee(string Name, string ID)
{
this.Name = Name;
this.ID = ID;
}
}
public class HR : Employee
{
public HR(string Name, string ID) : base(Name, ID)
{
}
public HR() : base("No name", "No ID")
{
}
}
public class IT : Employee
{
public IT(string Name, string ID) : base(Name, ID)
{
}
public IT() : base("No name", "No ID")
{
}
}
我为序列化程序添加了默认(无参数)构造函数。
然后你必须有某种包装器对象来处理Employee
的列表:
public class Employees
{
[XmlElement(typeof(IT))]
[XmlElement(typeof(HR))]
public List<Employee> Employee { get; set; } //It doesn't really matter what this field is named, it takes the class name in the serialization
}
接下来,您可以使用我的评论中的序列化代码生成XML:
var employees = new Employees
{
Employee = new List<Employee>()
{
new IT("Sugan", "88"),
new HR("Niels", "41")
}
};
var serializer = new XmlSerializer(typeof(Employees));
var xml = "";
using (var sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw))
{
serializer.Serialize(writer, employees);
xml = sw.ToString();
}
}
Console.WriteLine(xml);
(为清楚起见,省略了命名空间)
返回以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<IT>
<Name>Sugan</Name>
<ID>88</ID>
</IT>
<HR>
<Name>Niels</Name>
<ID>41</ID>
</HR>
</Employees>
答案 2 :(得分:1)
当我阅读你的xml时,似乎你想序列化一个Employee列表。 如果您的列表是类的成员(不直接序列化列表),我有一个解决方案。
public abstract class Employee
{
public string Name { get; set; }
public int ID { get; set; }
public Employee(string Name, int ID)
{
this.Name = Name;
this.ID = ID;
}
}
public class HR : Employee
{
public HR() : base(null, 0) { } // default constructor is needed for serialization/deserialization
public HR(string Name, int ID) : base(Name, ID) { }
}
public class IT : Employee
{
public IT() : base(null, 0) { }
public IT(string Name, int ID) : base(Name, ID) { }
}
public class Group
{
[XmlArray("Employee")]
[XmlArrayItem("HR",typeof(HR))]
[XmlArrayItem("IT",typeof(IT))]
public List<Employee> list { get; set; }
public Group()
{
list = new List<Employee>();
}
}
class Program
{
static void Main(string[] args)
{
Group grp = new Group();
grp.list.Add(new HR("Name HR", 1));
grp.list.Add(new IT("Name IT", 2));
XmlSerializer ser = new XmlSerializer(typeof(Group));
ser.Serialize(Console.Out, grp);
}
}
输出是:
<?xml version="1.0" encoding="ibm850"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Employee>
<HR>
<Name>Name HR</Name>
<ID>1</ID>
</HR>
<IT>
<Name>Name IT</Name>
<ID>2</ID>
</IT>
</Employee>
</Group>
非常类似于您想要的输出,除了根“组”的另外一个元素。
使用相同的XmlSerializer(typeof(Group))进行反序列化也应该可以正常工作。