.NET XML序列化陷阱? [英] .NET XML serialization gotchas?

查看:127
本文介绍了.NET XML序列化陷阱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做C#XML序列化时遇到一些陷阱
我想我会分享:

I've run into a few gotchas when doing C# XML serialization that I thought I'd share:


  • 您不能序列化是只读(如KeyValuePairs)项目

  • 您不能序列化一个通用词典。相反,试试这个包装类(从<一个href=\"http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx\">http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx):

  • You can't serialize items that are read-only (like KeyValuePairs)
  • You can't serialize a generic dictionary. Instead, try this wrapper class (from http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx):
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{      
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();

        if (wasEmpty)
            return;

        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");

            reader.ReadStartElement("key");
            TKey key = (TKey)keySerializer.Deserialize(reader);
            reader.ReadEndElement();

            reader.ReadStartElement("value");
            TValue value = (TValue)valueSerializer.Deserialize(reader);
            reader.ReadEndElement();

            this.Add(key, value);

            reader.ReadEndElement();
            reader.MoveToContent();
        }
        reader.ReadEndElement();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement("item");

            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();

            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
    }
}

任何其他的XML序列化的陷阱了吗?

Any other XML Serialization gotchas out there?

推荐答案

另一个巨大的疑难杂症:通过网页(ASP.NET)输出XML时,你不想包含的Uni$c$c字节顺序标记。当然,方式,使用或不使用BOM的几乎是相同的:

Another huge gotcha: when outputting XML through a web page (ASP.NET), you don't want to include the Unicode Byte-Order Mark. Of course, the ways to use or not use the BOM are almost the same:

XmlTextWriter wr = new XmlTextWriter(stream, new System.Text.Encoding.UTF8);

GOOD:

XmlTextWriter  wr = new XmlTextWriter(stream, new System.Text.UTF8Encoding(false))

您可以明确地传递false,表示你不想要的BOM。注意之间的明确,明显的区别 Encoding.UTF8 UTF8Encoding

You can explicitly pass false to indicate you don't want the BOM. Notice the clear, obvious difference between Encoding.UTF8 and UTF8Encoding.

在三个额外的BOM字节的开头是:(0xEFBBBF)或(239 187 191)。

The three extra BOM Bytes at the beginning are (0xEFBBBF) or (239 187 191).

参考:<一href=\"http://chrislaco.com/blog/troubleshooting-common-problems-with-the-xmlserializer/\">http://chrislaco.com/blog/troubleshooting-common-problems-with-the-xmlserializer/

这篇关于.NET XML序列化陷阱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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