我在命名空间学校下有一个简单的学生类。
namespace XmlTestApp
{
public class Student
{
private string studentId;
public string FirstName;
public string MI;
public string LastName;
public Student()
{
//Just provided for making Serialization work as obj.GetType() needs parameterless constructor.
}
public Student(String studentId)
{
this.studentId = studentId;
}
}
}
现在,当我序列化它时,我将它作为序列化的 xml:
<?xml version="1.0" encoding="utf-8"?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FirstName>Cad</FirstName>
<MI>Dsart</MI>
<LastName>dss</LastName>
</Student>
但我想要的是这个,基本上我需要在xml中以类名为前缀的命名空间,这可能吗?
<?xml version="1.0" encoding="utf-8"?>
<XmlTestApp:Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FirstName>Cad</FirstName>
<MI>Dsart</MI>
<LastName>dss</LastName>
</Student>
这是我的序列化代码:
Student s = new Student("2");
s.FirstName = "Cad";
s.LastName = "dss";
s.MI = "Dsart";
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(s.GetType());
TextWriter txtW=new StreamWriter(Server.MapPath("~/XMLFile1.xml"));
x.Serialize(txtW,s);