c# - 使用 XmlDictionaryWriter.CreateBinaryWriter 和 XmlDictionary 编写紧凑的 xml

标签 c# .net xml serialization binary-xml

我想以压缩格式将 xml 文档写入磁盘。为此,我使用了net框架的方法XmlDictionaryWriter.CreateBinaryWriter(Stream stream,IXmlDictionary dictionary)

此方法写入一个自定义的紧凑二进制 xml 表示,以后可以由 XmlDictionaryWriter.CreateBinaryReader 读取。该方法接受一个可以包含常用字符串的 XmlDictionary,这样就不必每次都在输出中打印这些字符串。字典索引将代替字符串打印在文件中。 CreateBinaryReader 稍后可以使用相同的字典来反转该过程。

但是我传递的词典显然没有被使用。考虑这段代码:

using System.IO;
using System.Xml;
using System.Xml.Linq;

class Program
{
    public static void Main()
    {
        XmlDictionary dict = new XmlDictionary();
        dict.Add("myLongRoot");
        dict.Add("myLongAttribute");
        dict.Add("myLongValue");
        dict.Add("myLongChild");
        dict.Add("myLongText");

        XDocument xdoc = new XDocument();
        xdoc.Add(new XElement("myLongRoot",
                                new XAttribute("myLongAttribute", "myLongValue"),
                                new XElement("myLongChild", "myLongText"),
                                new XElement("myLongChild", "myLongText"),
                                new XElement("myLongChild", "myLongText")
                                ));

        using (Stream stream = File.Create("binaryXml.txt"))
        using (var writer = XmlDictionaryWriter.CreateBinaryWriter(stream, dict))
        {
            xdoc.WriteTo(writer);
        }
    }
}

生成的输出是这样的(二进制控制字符未显示)

@
myLongRootmyLongAttribute˜myLongValue@myLongChild™
myLongText@myLongChild™
myLongText@myLongChild™
myLongText

很明显 XmlDictionary 还没有被使用。所有字符串都完整地出现在输出中,甚至出现多次。

这不是仅限于 XDocument 的问题。在上面的最小示例中,我使用 XDocument 来演示问题,但最初我是在将 XmlDictionaryWriter 与 DataContractSerializer 结合使用时偶然发现的,因为它很常用。结果是一样的:

[Serializable]
public class myLongChild
{
    public double myLongText = 0;
}
...
using (Stream stream = File.Create("binaryXml.txt"))
using (var writer = XmlDictionaryWriter.CreateBinaryWriter(stream, dict))
{
    var dcs = new DataContractSerializer(typeof(myLongChild));
    dcs.WriteObject(writer, new myLongChild());
}

生成的输出没有使用我的 XmlDictionary。

如何让 XmlDictionaryWriter 使用提供的 XmlDictionary?

还是我误解了它的工作原理?

使用 DataContractSerializer 方法,我尝试调试网络框架代码(visual studio/options/debugging/enable net.framework source stepping)。显然,Writer 确实尝试按预期在字典中查找上述每个字符串。但是在 line 356 of XmlbinaryWriter.cs 中查找失败,出于我不清楚的原因。

我考虑过的备选方案:

  • XmlDictionaryWriter.CreatebinaryWriter 有一个重载,它也接受 XmlBinaryWriterSession。然后编写器将它遇到的任何新字符串添加到 session 字典中。但是,我只想使用事先已知的静态字典进行读写

  • 我可以将整个内容包装到 GzipStream 中,并让压缩处理字符串的多个实例。但是,这不会压缩每个字符串的第一个实例,总体上看起来是一个笨拙的解决方法。

最佳答案

是的,有一个误解。 XmlDictionaryWriter 主要用于对象的序列化,它是XmlWriter 的子类。 XDocument.WriteTo(XmlWriter something)XmlWriter 作为参数。调用 XmlDictionaryWriter.CreateBinaryWriter 将在内部创建 System.Xml.XmlBinaryNodeWriter 的实例。此类具有“常规”写作的两种方法:

// override of XmlWriter
public override void WriteStartElement(string prefix, string localName)
{
  // plain old "xml" for me please
}

对于基于字典的方法:

// override of XmlDictionaryWriter
public override void WriteStartElement(string prefix, XmlDictionaryString localName)
{
  // I will use dictionary to hash element names to get shorter output
}

后者主要用于通过 DataContractSerializer 序列化对象(注意其方法 WriteObject 接受 XmlDictionaryWriterXmlWriter 的参数 类型),而 XDocument 只需要 XmlWriter

至于您的问题 - 如果我是您,我会制作自己的 XmlWriter:

class CustomXmlWriter : XmlWriter
{
  private readonly XmlDictionaryWriter _writer;
  public CustomXmlWriter(XmlDictionaryWriter writer)
  {
    _writer = writer;
  }
  // override XmlWriter methods to use the dictionary-based approach instead
}

更新(根据您的评论)

如果您确实使用了 DataContractSerializer,那么您的代码中几乎没有错误。

1)POC类必须用[DataContract][DataMember]属性修饰,序列化的值应该是property而不是field;还将 namespace 设置为空值,否则您还必须处理字典中的 namespace 。喜欢:

namespace  XmlStuff {
  [DataContract(Namespace = "")]
  public class myLongChild
  {
    [DataMember]
    public double myLongText { get; set; }
  }

  [DataContract(Namespace = "")]
  public class myLongRoot
  {
    [DataMember]
    public IList<myLongChild> Items { get; set; }
  }
}

2) 同时提供 session 实例;对于空 session ,字典编写器使用默认(XmlWriter-like)实现:

// order matters - add new items only at the bottom
static readonly string[] s_Terms = new string[]
{
    "myLongRoot", "myLongChild", "myLongText", 
    "http://www.w3.org/2001/XMLSchema-instance", "Items"
};

public class CustomXmlBinaryWriterSession : XmlBinaryWriterSession
{
  private bool m_Lock;
  public void Lock() { m_Lock = true; }

  public override bool TryAdd(XmlDictionaryString value, out int key)
  {
    if (m_Lock)
    {
      key = -1;
      return false;
    }

    return base.TryAdd(value, out key);
  }
}

static void InitializeWriter(out XmlDictionary dict, out XmlBinaryWriterSession session)
{
  dict = new XmlDictionary();
  var result = new CustomXmlBinaryWriterSession();
  var key = 0;
  foreach(var term in s_Terms)
  {
    result.TryAdd(dict.Add(term), out key);
  }
  result.Lock();
  session = result;
}

static void InitializeReader(out XmlDictionary dict, out XmlBinaryReaderSession session)
{
  dict = new XmlDictionary();
  var result = new XmlBinaryReaderSession();
  for (var i = 0; i < s_Terms.Length; i++)
  {
    result.Add(i, s_Terms[i]);
  }
  session = result;
}

static void Main(string[] args)
{
  XmlDictionary dict;
  XmlBinaryWriterSession session;
  InitializeWriter(out dict, out session);

  var root = new myLongRoot { Items = new List<myLongChild>() };
  root.Items.Add(new myLongChild { myLongText = 24 });
  root.Items.Add(new myLongChild { myLongText = 25 });
  root.Items.Add(new myLongChild { myLongText = 27 });

  byte[] buffer;
  using (var stream = new MemoryStream())
  {
    using (var writer = XmlDictionaryWriter.CreateBinaryWriter(stream, dict, session))
    {
      var dcs = new DataContractSerializer(typeof(myLongRoot));
      dcs.WriteObject(writer, root);
    }
    buffer = stream.ToArray();
  }


  XmlBinaryReaderSession readerSession;
  InitializeReader(out dict, out readerSession);
  using (var stream = new MemoryStream(buffer, false))
  {
    using (var reader = XmlDictionaryReader.CreateBinaryReader(stream, dict, new XmlDictionaryReaderQuotas(), readerSession))
    {
      var dcs = new DataContractSerializer(typeof(myLongRoot));
      var rootCopy = dcs.ReadObject(reader);
    }
  }
}    

关于c# - 使用 XmlDictionaryWriter.CreateBinaryWriter 和 XmlDictionary 编写紧凑的 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35271458/

相关文章:

xml - 如何在 Spring MVC 中配置自定义 MediaType?

javascript - 使用 JavaScript XHR 列出服务器目录

c# - 从 Ubuntu 中的其他 C# 服务访问自签名 HTTPS C# 服务

c# - 如何从 C# 更改 IIS 中应用程序池的用户名/密码?

c# - 谁能解释一下 OAuth?

.NET PInvoke 异常处理

xml - 解析具有相同名称的嵌套 XML 标记

c# - 编译时从数据库获取dll版本号

c# - 指定 xUnit Theory 测试的返回值

c# - 如何在 C# 中将对象转换为 Dictionary<TKey, TValue>?