【问题标题】:OmitXmlDeclaration in XmlWriter and implementing IXmlSerializable在 XmlWriter 中省略 XmlDeclaration 并实现 IXmlSerializable
【发布时间】:2011-10-11 07:58:25
【问题描述】:

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

[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 上实现 IXmlSerializable ?

【问题讨论】:

  • 注意你不需要[Serializable]

标签: c# .net xml xml-serialization ixmlserializable


【解决方案1】:

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

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

此外,您不需要实现IXmlSerializable。你不能在内心层面做到这一点——为时已晚。

例如:

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" />

【讨论】:

    猜你喜欢
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多