Win Coder
Win Coder

Reputation: 6756

Writing all the content of a dictionary to a file

I have a dictionary that looks something like this

public Dictionary<string,List<ForwardBarrelRecord>> lexicon = new Dictionary<string, List<ForwardBarrelRecord>>();

with ForwardBarrelRecord looking like this

public struct ForwardBarrelRecord
{
    public string DocId;
    public int hits { get; set; }
    public List<int> hitLocation;
}

I want to write everything down to the list of int in the forward barrel record on a file. So that when i retrieve it i can make an exact reconstruction of the dictionary.

So far i have written the code but it only saves the key in the dictionary and instead of making a copy of value just writes the class path. So far my code is

using (var file = new System.IO.StreamWriter("myfile.txt"))
        {
            foreach (var entry in pro.lexicon)
            {
                file.WriteLine("[{0} {1}]", entry.Key, entry.Value);
            }
        }

I am looking to make a deep copy of everything in this dictionary of mine.

Any help would be appreciated.

Upvotes: 0

Views: 489

Answers (1)

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13765

As stated in this link Why isn't there an XML-serializable dictionary in .NET?

The thing about XML Serialization is that it's not just about creating a stream of bytes. It's also about creating an XML Schema that this stream of bytes would validate against. There's no good way in XML Schema to represent a dictionary. The best you could do is to show that there's a unique key

but if you want a work arround you can try this code I tried and it's work very well one thing that you should do Manually is to check that the key is always unique try something like this

 class Program
{        
    static void Main(string[] args)
    {

        List<KeyValuePair<string,List<ForwardBarrelRecord>>>  lexicon   = new List<KeyValuePair<string,List<ForwardBarrelRecord>>>();  
        ForwardBarrelRecord FBR = new ForwardBarrelRecord();  
        FBR.DocId ="12"; 
        FBR.hits= 14;  
        FBR.hitLocation = new List<int>(){12,13,114};
        var lst = new List<ForwardBarrelRecord>() { FBR, FBR };
        KeyValuePair<string,List<ForwardBarrelRecord>> t= new KeyValuePair<string,List<ForwardBarrelRecord>>("Test",lst);
        lexicon.Add(t);            
        XmlSerializer serializer = new XmlSerializer(typeof(List<KeyValuePair<string, List<ForwardBarrelRecord>>>));
        string  fileName= @"D:\test\test.xml";
        Stream stream = new FileStream(fileName,FileMode.Create);
        serializer.Serialize(stream,lexicon);
        stream.Close();            
    }     
}

public struct ForwardBarrelRecord
{
    [XmlElement]
    public string DocId;
    [XmlElement]
    public int hits { get; set; }
    [XmlElement]
    public List<int> hitLocation;
}

} but if you want a more robust solution you can use this customized SortedDictionary http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx

hope this help

Upvotes: 1

Related Questions