0

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.

Win Coder
  • 6,628
  • 11
  • 54
  • 81
  • You can override `ToString` method to get a string representation of your object, also it would be better if you save the data in XML format rather than Text file, it would give you easier options to handle, IMO – Habib Oct 19 '13 at 18:17
  • Consider **Serialization** as an alternative – Alireza Oct 19 '13 at 18:19
  • 1
    You should see here: http://stackoverflow.com/questions/1347375/c-sharp-object-dumper – nawfal Oct 19 '13 at 18:23
  • If you are looking for "deep copy" (deep clone) - there are plenty of questions that discuss that. Writing as text may not be the best approach in this case - I believe binary serialization is faster and easier. – Alexei Levenkov Oct 19 '13 at 18:37

1 Answers1

1

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

Community
  • 1
  • 1
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47