I have the following XML file:
<myXML>
<MyGroup>
<source>OCU</source>
<endianity>
<set>LITTLE</set><!--LITTLE/BIG Endian-->
</endianity>
<msgNumber>
<Number>5</Number>
<Field>
<name>Pos_X</name>
<entry>
<ByteOffset>
<offset>8</offset>
</ByteOffset>
<ByteSize>
<Size>4</Size>
</ByteSize>
</entry>
</Field>
<Field>
<name>Pos_Y</name>
<entry>
<ByteOffset>
<offset>12</offset>
</ByteOffset>
<ByteSize>
<Size>4</Size>
</ByteSize>
</entry>
</Field>
</msgNumber>
</MyGroup>
</myXML>
I parsed it to an object using XmlSerializer and it worked. The only problem i had to create a class that matches that object. I mean something like this:
xmlInputData = File.ReadAllText(newPath + xmlFileName);
myXML messages = Deserialize<myXML>(xmlInputData);
I now want to work without creating a class - that means to parse the XML directly to a dictionary - something like the following:
Dictionary<MessageNumber, SecondDictionary>
|
|
SecondDictionary<FieldName, FieldValue>
How do i parse it using Nested Dictionaries? how to keep the same hierarchy from the XML?
EDIT:
I tried writing it in LINQ but it's too complicating for me:
var doc = XDocument.Load(newPath + xmlFileName);
var rootNodes = doc.Root.Descendants("msgNumber");
var keyValuePairs = from n in rootNodes
select new
{
TagName = n.Name,
TagValue = n.Value
};
Dictionary<string, string> allItems = new Dictionary<string, string>();
foreach (var token in keyValuePairs)
{
allItems.Add(token.TagName.ToString(), token.TagValue.ToString());
}
How do i get the desired hierarchy using LINQ? The main thing i want to accomplish is: For each message to obtain it fields.
For example, for msg number 5 i want to save all the fields (keep the hierarchy ). That means the name, and the entry (byte offset and byte size).