在 XmlWriter 中省略 XmlDeclaration 并实现 IXmlSerializable

发布于 2024-12-09 02:21:49 字数 1562 浏览 1 评论 0原文

我想通过实现 IXmlSerialized 创建自定义 xml 序列化。 我有一个实现 IXmlSerialized 接口的测试类:

[Serializable]
public class Employee : IXmlSerializable
{
    public Employee()
    {
        Name = "Vyacheslav";

        Age = 23;                 
    }

    public string Name{get; set;}

    public int Age { get; set; }



    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        this.Name = reader["Name"].ToString();
        this.Age = Int32.Parse(reader["Age"].ToString());
    }

    public void WriteXml(XmlWriter writer)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        settings.ConformanceLevel = ConformanceLevel.Fragment;

        XmlWriter newWriter = XmlWriter.Create(writer, settings);
        newWriter.WriteAttributeString("Name", this.Name);
        newWriter.WriteAttributeString("Age", this.Age.ToString());            
    }
}

我想要做的是省略 xml 声明。为此,我创建了 XmlWriterSettings 的正确实例并将其作为第二个参数传递以创建新的 XmlWriter。 但是当我调试这段代码时,我看到 newWriter.Settings.OmitXmlDeclaration 设置为 false 并且序列化数据包含标记。我做错了什么?

实际的序列化如下所示:

        var me = new Employee();

        XmlSerializer serializer = new XmlSerializer(typeof(Employee));
        TextWriter writer = new StreamWriter(@"D:\file.txt");
        serializer.Serialize(writer, me);
        writer.Close();

第二个问题是 - 如果我想序列化在要序列化的字段中具有自定义类型 ContactInfo 的 Employee 类型,我是否也需要在 ContactInfo 上实现 IXmlSerialized ?

I want to create custom xml serialization by implementing IXmlSerializable.
I've got this test class that implements IXmlSerializable interface:

[Serializable]
public class Employee : IXmlSerializable
{
    public Employee()
    {
        Name = "Vyacheslav";

        Age = 23;                 
    }

    public string Name{get; set;}

    public int Age { get; set; }



    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        this.Name = reader["Name"].ToString();
        this.Age = Int32.Parse(reader["Age"].ToString());
    }

    public void WriteXml(XmlWriter writer)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        settings.ConformanceLevel = ConformanceLevel.Fragment;

        XmlWriter newWriter = XmlWriter.Create(writer, settings);
        newWriter.WriteAttributeString("Name", this.Name);
        newWriter.WriteAttributeString("Age", this.Age.ToString());            
    }
}

What I want to do is to omit xml declaration. For that I create proper instance of XmlWriterSettings and pass it as second parameter to create new XmlWriter.
But when I debug this piece of code, I see that newWriter.Settings.OmitXmlDeclaration is set to false and serialized data contains tag. What am I doing wrong?

The actual serialization looks like this:

        var me = new Employee();

        XmlSerializer serializer = new XmlSerializer(typeof(Employee));
        TextWriter writer = new StreamWriter(@"D:\file.txt");
        serializer.Serialize(writer, me);
        writer.Close();

And the second question is - if I want to serialize type Employee that has cutom type ContactInfo at field to be serialized, do I need to implement IXmlSerializable on ContactInfo too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2024-12-16 02:21:49

writer-settings 是最外层 writer 的函数;您应该将其应用到创建文件的代码中,即

using(var file = File.Create("file.txt"))
using(var writer = XmlWriter.Create(file, settings))
{
    serializer.Serialize(writer, me);
}

另外,您不需要来实现IXmlSerialized。你无法在内在层面做到这一点——已经太晚了。

例如:

using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Employee 
{
    [XmlAttribute] public string Name { get; set; }
    [XmlAttribute] public int Age { get; set; }
}
class Program
{
    static void Main()
    {
        var settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        var me = new Employee {
            Name = "Vyacheslav", Age = 23
        };
        var serializer = new XmlSerializer(typeof (Employee));
        using (var file = File.Create("file.txt"))
        using (var writer = XmlWriter.Create(file, settings))
        {
            serializer.Serialize(writer, me);
        }
    }
}

如果您不需要额外的命名空间,那么:

        var ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        serializer.Serialize(writer, me, ns);

它将生成文件:

<Employee Name="Vyacheslav" Age="23" />

The writer-settings is a function of the outermost writer; you should be applying that to the code that creates the file, i.e.

using(var file = File.Create("file.txt"))
using(var writer = XmlWriter.Create(file, settings))
{
    serializer.Serialize(writer, me);
}

additionally, then, you don't need to implement IXmlSerializable. You cannot do this at the inner level - it is too late.

For example:

using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Employee 
{
    [XmlAttribute] public string Name { get; set; }
    [XmlAttribute] public int Age { get; set; }
}
class Program
{
    static void Main()
    {
        var settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        var me = new Employee {
            Name = "Vyacheslav", Age = 23
        };
        var serializer = new XmlSerializer(typeof (Employee));
        using (var file = File.Create("file.txt"))
        using (var writer = XmlWriter.Create(file, settings))
        {
            serializer.Serialize(writer, me);
        }
    }
}

and if you don't want the extra namespaces, then:

        var ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        serializer.Serialize(writer, me, ns);

which generates the file:

<Employee Name="Vyacheslav" Age="23" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文