来自字典>到XML [英] From Dictionary> to XML
问题描述
我有字典.
字典< string,>> Dic =新字典< string,>>();
项目
--- 1
--- 2
-3
Item2
.....
I have Dictionary.
Dictionary<string,>> Dic = new Dictionary<string,>>();
Item
--- 1
--- 2
--- 3
Item2
.....
How convert it to xml ?
推荐答案
没有转换";如何将其转换为xml?这个词本身没有任何意义.没有预定义的或标准的"方式来建立两者之间的对应关系.基于某些词典数据创建多种不同的XML的方法有无数种;缺少两条信息:必需的 XML模式和从一种数据表示形式到另一种形式的mapping
规则集.对我而言,您可能并不了解它的第一位,这意味着您并不真正知道自己到底想要什么.首先确定它,然后找到正确解释它的方法.
更好的是,了解XML(例如,从此处开始: http://en.wikipedia.org/wiki/XML [ ^ ])和XML生成和解析的.NET实现.这是我对这些主题的简短概述:
There is no "convert"; and the term itself makes no sense. There is not a predefined or "standard" way to establish correspondence between the two. There can be unlimited number of ways to create some different XMLs based on some dictionary data; there are two missing pieces of information: the required XML schema and the set ofmapping
rules from one form of data presentation to another one. As you did not understand that it first place, to me, it means you don''t really know what exactly you want. Decide it first and then find a way to explain it properly.
Better yes, learn about XML (start, for example, here: http://en.wikipedia.org/wiki/XML[^]) and .NET implementation of XML generation and parsing. This is my short overview of these topics:
- 使用
System.Xml.XmlDocument
类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
请参见 ^ ]. - 使用类
System.Xml.XmlTextWriter
和System.Xml.XmlTextReader
;这是最快的读取方法,尤其是您需要跳过一些数据.
请参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [http://msdn.microsoft.com/en-us/library/bb387063.aspx [
- Use
System.Xml.XmlDocument
class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^]. - Use the classes
System.Xml.XmlTextWriter
andSystem.Xml.XmlTextReader
; this is the fastest way of reading, especially is you need to skip some data.
See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^]. - Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that ofXmlDocument
, supporting LINQ to XML Programming.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].
经过OP在以下评论中的澄清:
如果只需要对字典或任何其他数据结构进行序列化/反序列化,则不需要直接使用XML(并且您不应该这样做或应用任何临时方法).而是使用数据合同:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].
使用数据协定,您可以序列化任意拓扑的任何对象图(如果打开循环引用支持,则不只是树).这种方法最容易使用,也最不介入:您无需更改数据类型来容纳它们,只需添加一些属性即可.
我在过去的一些回答中都主张采用这种方法:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [
After an OP''s clarification in comments below:
If you just need to serialize/deserialize a dictionary or any other data structure, you don''t need to work with XML directly (and you should not do it or apply any ad-hoc approach). Instead, use Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].
With Data Contracts, you can serialize any object graph of arbitrary topology (not just a tree, if you switch on the support of circular references). This approach is the easiest to use and the most non-intrusive: you don''t change your data types to accommodate them, you just add some attributes.
I advocated this approach in some of my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].
这篇关于来自字典>到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!