Click here to Skip to main content
16,004,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have Dictionary.
Dictionary<string,>> Dic = new Dictionary<string,>>();

Item
--- 1
--- 2
--- 3
Item2
.....

How convert it to xml ?
Posted
Comments
CodeHawkz 6-Sep-12 3:22am    
Dictionary<string,>> Dic = new Dictionary<string,>>(); <<< ????
How can you even compile this code unless you've made a mistake in copy pasting your code.

1 solution

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 of mapping 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:


  1. 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[^].
  2. Use the classes System.Xml.XmlTextWriter and System.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[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, 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[^].


[EDIT]

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...[^].

—SA
 
Share this answer
 
v2
Comments
[no name] 6-Sep-12 3:55am    
I know how working with xml/ I want serialize Dictionary
That's i find

XElement el = new XElement("root",
Dic.Select(kv => new XElement(kv.Key, kv.Value.ToString())));

This code not working(its from google)
Sergey Alexandrovich Kryukov 6-Sep-12 13:14pm    
If you really know how to work with XML, you should be able to serialize everything :-)

OK, I think you clarified your purpose now. In this case, please see my updated answer, after [EDIT]. This is all you really need.
Good luck,
--SA
Andrei Straut 6-Sep-12 13:20pm    
Good job SA!
Sergey Alexandrovich Kryukov 6-Sep-12 13:26pm    
Thank you, Andrei.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900