theString = @" <MgmtSystemInfo> <LocalFixeDisk> <Disks Drive="C:"> <Param Description="Local Fixed Disk" TimeStamp="4/26/2010 2:20:11 AM" /> <Param Compressed="No" TimeStamp="4/26/2010 2:20:11 AM" /> <Param FileSystem="NTFS" TimeStamp="4/26/2010 2:20:11 AM" /> <Param TotalSpace="149.05GB (160039239680bytes)" TimeStamp="4/26/2010 2:20:11 AM" /> <Param AvailableSpace="140.47GB (150827999232bytes)" TimeStamp="4/26/2010 2:20:11 AM" /> <Param PercentageAvaliable="94.24%" TimeStamp="4/26/2010 2:20:11 AM" /> <Param VolumeName="OS" TimeStamp="4/26/2010 2:20:11 AM" /> <Param VolumeSerialNumber="9C3F9B31" TimeStamp="4/26/2010 2:20:11 AM" /> </Disks> </LocalFixeDisk> <SystemInfo> <Param OSName="Microsoft Windows XP Professional" TimeStamp="4/26/2010 2:20:11 AM" /> <Param Version="5.1.2600 Service Pck 2 Build 2600" TimeStamps="4/26/2010 2:20:11 AM" /> <Param OSManufacturer="Microsoft Corporation" TimeStamps="4/26/2010 2:20:11 AM" /> <Param SN=" 1234567" TimeStamps="4/26/2010 2:20:11 AM" / <Param WindowsDirectory="C:\WINDOWS" TimeStamps="4/26/2010 2:20:11 AM" /> </SystemInfo> <MgmtSystemInfo>"
<Param Description="Local Fixed Disk" TimeStamp="4/26/2010 2:20:11 AM" />
<Param Compressed="No" TimeStamp="4/26/2010 2:20:11 AM" /> <Param FileSystem="NTFS" TimeStamp="4/26/2010 2:20:11 AM" />
class Param { public string Description { get; set; } public string Compressed { get; set; } public string FileSystem { get; set; } public string TotalSpace { get; set; } public string AvailableSpace { get; set; } public string PercentageAvailable { get; set; } public string Volumename { get; set; } public string VolumeSerialNum { get; set; } }; public class DiskDrives { public string Drive { get; set; } } XDocument xdoc = Xdocument.Parse(theString);
List<DiskDrives> LocalDisks = (from drives in xdoc.Descendants("Disks") select new DiskDrives { Drive = drives.Attribute("Drive").Value, }).ToList<DiskDrives>(); foreach (var drives in LocalDisks) { Console.WriteLine("Drive: " + drives.Drive.ToString()); var Parameters = from theparam in xdoc.Descendants("Disks") where (theparam.Attribute("Drive").Value == drives.Drive) select new Param { Description = theparam.Element("Param").Attribute("Description").Value, // Note, I had to comment the next two lines out as they were causing // exceptions.// My intent is to capture the rest of the attributes in the // above XML string // Compressed = theparam.Element("Param").Attribute("Compressed").Value, //FileSystem = theparam.Element("Param").Attribute("FileSystem").Value, }; foreach (var item in Parameters) { Console.WriteLine("Item: " + item.Description); // I get the correct value here "Local Fixed Disk" Console.WriteLine("Item: " + item.Compressed); // The value should be "No" Console.WriteLine("Item: " + item.FileSystem); // The value should be "NTFS"} }
List<DiskDrives> LocalDisks = (from drives in