It sounds like you're searching for files, not sections. So your LINQ query should return the file element that matches the ID you want. From there you can access its attributes and its child sections.
I modified your code to use a helper type to handle the XML document as I find it cleaner. Personally I would also move the XML parsing to this helper class and leave the original data classes clean but that is a preference. The hardest issue here is that your FileID
is a hex number so you have to convert it properly.
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 ( XElement element )
{
//Parse the data now or look it up as needed...
_element = element;
}
private readonly XElement _element;
}
public class FileXmlDocument
{
public FileXmlDocument ( string filename )
{
_doc = XDocument.Load(filename);
}
public MyFiletype GetFile ( UInt32 id )
{
var element = _doc.Descendants("File").FirstOrDefault(x => ParseHexValue(x.Attribute("FileID")?.Value) == id);
return (element != null) ? new MyFiletype(element) : null;
}
private uint ParseHexValue ( string value )
{
if (String.IsNullOrEmpty(value))
return 0;
if (value.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
return UInt32.TryParse(value.Substring(2), NumberStyles.HexNumber, null, out var result2) ? result2 : 0;
return UInt32.TryParse(value, out var result) ? result : 0;
}
private readonly XDocument _doc;
}