Hello everyone,
This is my first time to learn LINQ and I need to be able to query data quickly and store the information in an object. Not sure which thread to post this to, but I think C-Sharp Programming should be the correct thread.
Anyway, I have the following string :
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>"
|
I need to be able to read each Param attribute from say, the <Disks> </Disks> section of this XML string.
For instance, i want to be able to read the Description attribute in this section of the above XML :
<Param Description="Local Fixed Disk" TimeStamp="4/26/2010 2:20:11 AM" />
|
and then programatically read the rest of the attributes in each PARAM section, say, the Compressed and FileSystem attributes, etc.
<Param Compressed="No" TimeStamp="4/26/2010 2:20:11 AM" /> <Param
FileSystem="NTFS" TimeStamp="4/26/2010 2:20:11 AM" />
|
and so on...
How do I do this ?
I tried this snippet as an example but it does not work ( I get an exception when I added the commented out lines below )
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" } }
|
In the above code, the commented out lines cause exceptions.
I am able to get the Description attribute of the first <PARAM />
section, but do not know how I can get the other attributes programatically ( e.g., the Compressed and FileSystem attributes ).
Your help/advise regarding this matter will be highly appreciated.