M
mp
Trying to implement a persistant storage method for a dictionary of
dictionaries
at run time a dictionary is filled with sorted dictionaries
at some point in prog i want to store existing data to disk
at next program run dictionaries could be refilled with data from disk
first thought is use xml
open to other suggestions
here is first implementation...it works but is there a better way or are
there any constructive criticisms of this method
m_profilesDict is defined at module level as dictionary(Of String, Object)
it is filled previous to calling this sub
this sub could also receive a dict obj as arg in lieu of private member var
sorry the news reader removed indenting, hope it's readable
Sub TestDictionaryToXml()
Dim xmlDoc As XmlDocument = New XmlDocument()
Dim XmlDecl As XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8",
String.Empty)
' Create the root element
Dim rootNode As XmlElement = xmlDoc.CreateElement("ProfilesList")
xmlDoc.InsertBefore(XmlDecl, xmlDoc.DocumentElement)
xmlDoc.AppendChild(rootNode)
Dim someDict As SortedDictionary(Of Double, String)
'<ProfileList>
' <Profile ID="01">
' <Length>24.625</Length>
' <Pcmk>3</Pcmk>
' </Profile>
'</ProfileList>
Dim LengthNode As XmlElement
Dim LengthText As XmlText
Dim CodeNode As XmlElement
Dim CodeText As XmlText
Dim parentNode As XmlElement
For Each kvp As KeyValuePair(Of String, Object) In m_profilesDict
' Create a new <Profile> element and add it to the root node
parentNode = xmlDoc.CreateElement("Profile")
' Set attribute name and value!
parentNode.SetAttribute("ID", kvp.Key)
xmlDoc.DocumentElement.PrependChild(parentNode)
'kvp.Value
someDict = kvp.Value
For Each kvp2 As KeyValuePair(Of Double, String) In someDict
' Create the required nodes
LengthNode = xmlDoc.CreateElement("LENGTH")
LengthText = xmlDoc.CreateTextNode(kvp2.Key)
' append the nodes to the parentNode without the value
parentNode.AppendChild(LengthNode)
' save the value of the fields into the nodes
LengthNode.AppendChild(LengthText)
CodeNode = xmlDoc.CreateElement("PCMK")
CodeText = xmlDoc.CreateTextNode(kvp2.Value)
' append the nodes to the parentNode without the value
parentNode.AppendChild(CodeNode)
' save the value of the fields into the nodes
CodeNode.AppendChild(CodeText)
Next kvp2
Next kvp
' Save to disk
xmlDoc.Save("PathToFile\FileName.xml")
End Sub
Thanks for any tips
mark
dictionaries
at run time a dictionary is filled with sorted dictionaries
at some point in prog i want to store existing data to disk
at next program run dictionaries could be refilled with data from disk
first thought is use xml
open to other suggestions
here is first implementation...it works but is there a better way or are
there any constructive criticisms of this method
m_profilesDict is defined at module level as dictionary(Of String, Object)
it is filled previous to calling this sub
this sub could also receive a dict obj as arg in lieu of private member var
sorry the news reader removed indenting, hope it's readable
Sub TestDictionaryToXml()
Dim xmlDoc As XmlDocument = New XmlDocument()
Dim XmlDecl As XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8",
String.Empty)
' Create the root element
Dim rootNode As XmlElement = xmlDoc.CreateElement("ProfilesList")
xmlDoc.InsertBefore(XmlDecl, xmlDoc.DocumentElement)
xmlDoc.AppendChild(rootNode)
Dim someDict As SortedDictionary(Of Double, String)
'<ProfileList>
' <Profile ID="01">
' <Length>24.625</Length>
' <Pcmk>3</Pcmk>
' </Profile>
'</ProfileList>
Dim LengthNode As XmlElement
Dim LengthText As XmlText
Dim CodeNode As XmlElement
Dim CodeText As XmlText
Dim parentNode As XmlElement
For Each kvp As KeyValuePair(Of String, Object) In m_profilesDict
' Create a new <Profile> element and add it to the root node
parentNode = xmlDoc.CreateElement("Profile")
' Set attribute name and value!
parentNode.SetAttribute("ID", kvp.Key)
xmlDoc.DocumentElement.PrependChild(parentNode)
'kvp.Value
someDict = kvp.Value
For Each kvp2 As KeyValuePair(Of Double, String) In someDict
' Create the required nodes
LengthNode = xmlDoc.CreateElement("LENGTH")
LengthText = xmlDoc.CreateTextNode(kvp2.Key)
' append the nodes to the parentNode without the value
parentNode.AppendChild(LengthNode)
' save the value of the fields into the nodes
LengthNode.AppendChild(LengthText)
CodeNode = xmlDoc.CreateElement("PCMK")
CodeText = xmlDoc.CreateTextNode(kvp2.Value)
' append the nodes to the parentNode without the value
parentNode.AppendChild(CodeNode)
' save the value of the fields into the nodes
CodeNode.AppendChild(CodeText)
Next kvp2
Next kvp
' Save to disk
xmlDoc.Save("PathToFile\FileName.xml")
End Sub
Thanks for any tips
mark