Hi all,
Frustrated, having a tough time with linq ...
Read only xml configuration file with IList<> of objects.
The goal is to pass in an id (by constructor or routine), it reads that id's configuration from xml into properties of this class that can be publicly read.
Design is open, this is the rut my mind is currently in ...
<Root>
<File FileID="0x01" Size="0x00034100" Description="Test 1 Description">
<Sections>
<Section Index="1" Start="0x00000000" Size="0x00000100">Description 1</Section>
<Section Index="2" Start="0x00000100" Size="0x00001000">Description 2</Section>
<Section Index="3" Start="0x00001100" Size="0x00010000">Description 3</Section>
</Sections>
</File>
<File FileID="0x02" Size="0x00031100" Description="Test 2 Description">
<Sections>
<Section Index="1" Start="0x00000000" Size="0x00000100">1 Description</Section>
<Section Index="2" Start="0x00000100" Size="0x00001000">2 Description</Section>
<Section Index="3" Start="0x00001100" Size="0x00010000">3 Description</Section>
</Sections>
</File>
</Root>
public class Section
{
// Note: The collection must remain in order, if the list keeps order, we don't need Index
public UInt32 Index { get; set; } // Node Section Attribute("Index")
public UInt32 Start { get; set; } // Node Section Attribute("Start")
public UInt32 Size { get; set; } // Node Section Attribute("Size")
public string Description { get; set; } // Node Section Value
}
public class MyFiletype
{
public UInt32 FileID { get; set; } // Node File Attribute("FileID")
public UInt32 Size { get; set; } // Node File Attribute("Size")
public string Description { get; set; } // Node File Attribute("Description")
public IList<Section> Sections { get; set; } // Collection of Section objects
public MyFiletype(UInt32 id)
{
string filename = @"..\..\Xml\Sections.xml";
var doc = XDocument.Load(filename); // XDocument
var result = doc.Descendants("File") // IEnumerable<XElement>
// Find the chosen node, we only want one and it's descendants!
.Where(i => Convert.ToUInt32(i.Attribute("FileID").Value, 16) == id)
// This is where I get stumped ... Have read and tried so many ways I think I'm even more lost!
// I can get one or the other, not both Node File's Attributes and the Sections Collection.
.FirstOrDefault().Value; // test bit garbage
}
}
/*
// It's use ...
UInt32 id = 0x02;
MyFiletype myclass = new MyFiletype(id);
//Debug.WriteLine($"File Description: {myclass.Description}");
*/
Thank you
fh