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

Hug*_*une 5 .net c# xml serialization binary-xml

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

\n\n

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

\n\n

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

\n\n
using System.IO;\nusing System.Xml;\nusing System.Xml.Linq;\n\nclass Program\n{\n    public static void Main()\n    {\n        XmlDictionary dict = new XmlDictionary();\n        dict.Add("myLongRoot");\n        dict.Add("myLongAttribute");\n        dict.Add("myLongValue");\n        dict.Add("myLongChild");\n        dict.Add("myLongText");\n\n        XDocument xdoc = new XDocument();\n        xdoc.Add(new XElement("myLongRoot",\n                                new XAttribute("myLongAttribute", "myLongValue"),\n                                new XElement("myLongChild", "myLongText"),\n                                new XElement("myLongChild", "myLongText"),\n                                new XElement("myLongChild", "myLongText")\n                                ));\n\n        using (Stream stream = File.Create("binaryXml.txt"))\n        using (var writer = XmlDictionaryWriter.CreateBinaryWriter(stream, dict))\n        {\n            xdoc.WriteTo(writer);\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
@\nmyLongRootmyLongAttribute\xcb\x9cmyLongValue@myLongChild\xe2\x84\xa2\nmyLongText@myLongChild\xe2\x84\xa2\nmyLongText@myLongChild\xe2\x84\xa2\nmyLongText\n
Run Code Online (Sandbox Code Playgroud)\n\n

显然 XmlDictionary 尚未被使用。所有字符串都会完整地出现在输出中,甚至多次出现。

\n\n

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

\n\n
[Serializable]\npublic class myLongChild\n{\n    public double myLongText = 0;\n}\n...\nusing (Stream stream = File.Create("binaryXml.txt"))\nusing (var writer = XmlDictionaryWriter.CreateBinaryWriter(stream, dict))\n{\n    var dcs = new DataContractSerializer(typeof(myLongChild));\n    dcs.WriteObject(writer, new myLongChild());\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

如何让 XmlDictionaryWriter 使用提供的 XmlDictionary?

\n\n

或者我误解了它是如何工作的?

\n\n

使用 DataContractSerializer 方法,我尝试调试网络框架代码(Visual Studio/options/debugging/enable net.framework 源代码步进)。显然,作者确实按照预期尝试在字典中查找上述每个字符串。然而,查找在XmlbinaryWriter.cs 的第 356 行失败,原因我不清楚。

\n\n

我考虑过的替代方案:

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

  • \n
  • 我可以将整个内容包装成 aGzipStream并让压缩处理字符串的多个实例。但是,这不会压缩每个字符串的第一个实例,并且总体而言似乎是一个笨拙的解决方法。

  • \n
\n

Ond*_*dar 3

是的,有一个误会。XmlDictionaryWriter主要用于对象的序列化,它是 的子类XmlWriterXDocument.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
}
Run Code Online (Sandbox Code Playgroud)

对于基于字典的方法:

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

后者主要用于通过序列化对象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
}
Run Code Online (Sandbox Code Playgroud)

更新(根据您的评论)

如果你确实使用DataContractSerializer你的代码中几乎不会有错误。

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

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; }
  }
}
Run Code Online (Sandbox Code Playgroud)

2)同时提供会话实例;对于空会话,字典编写器使用默认(XmlWriter类似)实现:

// 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);
    }
  }
}    
Run Code Online (Sandbox Code Playgroud)