在 C# 中将对象序列化为 xml [英] Serializing objects to xml in C#

查看:28
本文介绍了在 C# 中将对象序列化为 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在命名空间 School 下有一个简单的类 Student.

I have a simple class Student under namespace School.

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:

Now when i serialize this, i get this as serialized 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中以类名为前缀的命名空间,这可能吗?

But what i want is this, basically i need the namespace prefixed to class name in xml, is this possible?

<?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);

推荐答案

EDIT:简短的回答仍然是肯定的.正确的属性实际上是 XmlType 属性.此外,您需要指定一个命名空间,然后在序列化代码中,您需要为将用于限定元素的命名空间指定别名.

EDIT: Short answer is still yes. The proper attribute is actually the XmlType attribute. In addition, you will need to specify a namespace, and then in the serialization code you will need to specify aliases for the namespaces that will be used to qualitfy elements.

namespace XmlTestApp
{
    [XmlRoot(Namespace="xmltestapp", TypeName="Student")]
    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;
        }

    }
}

...

        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());

        System.Xml.Serialization.XmlSerializationNamespaces ns = new System.Xml.Serialization.XmlSerializationNamespaces();

        ns.Add("XmlTestApp", "xmltestapp");

        TextWriter txtW=new StreamWriter(Server.MapPath("~/XMLFile1.xml"));
        x.Serialize(txtW,s, ns); //add the namespace provider to the Serialize method

您可能需要尝试设置命名空间以确保它仍然使用来自 W3.org 的 XSD/XSI,但这应该会让您走上正轨.

You may have to play around with the setting up of the namespace to ensure it still uses the XSD/XSI from W3.org, but this should get you on the right track.

这篇关于在 C# 中将对象序列化为 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆