Jump to content

LINQ: How to Read XML PARAMETER Attributes

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
SeekAndFind

SeekAndFind

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#2
lobo521

lobo521

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
// 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,

Element("Name") returns first element with that name. First element has no "Compressed" or "FileSystem" attribute only "Description".

theparam.Attributes("Compressed") should return IEnumerable of "Compressed" attributes in elements descendant to theparam element.
If attributes are distinct You can call theparam.Attributes("Compressed").Single().

IMO xml should be like that:

<Disks>
<Disk>
<DriveName="C:" />
<Description="Local Fixed Disk" TimeStamp="4/26/2010 2:20:11 AM" />
<Compressed="No" TimeStamp="4/26/2010 2:20:11 AM" />
<FileSystem="NTFS" TimeStamp="4/26/2010 2:20:11 AM" />
<TotalSpace="149.05GB (160039239680bytes)" TimeStamp="4/26/2010 2:20:11 AM" />
<AvailableSpace="140.47GB (150827999232bytes)" TimeStamp="4/26/2010 2:20:11 AM" />
<PercentageAvaliable="94.24%" TimeStamp="4/26/2010 2:20:11 AM" />
<VolumeName="OS" TimeStamp="4/26/2010 2:20:11 AM" />
<VolumeSerialNumber="9C3F9B31" TimeStamp="4/26/2010 2:20:11 AM" />
</Disk>
</Disks>

It would be easier to use it.

#3
SeekAndFind

SeekAndFind

    Newbie

  • Members
  • Pip
  • 3 posts
[QUOTE=lobo521;252826IMO xml should be like that:

<Disks>
<Disk>
<DriveName="C:" />
<Description="Local Fixed Disk" TimeStamp="4/26/2010 2:20:11 AM" />
<Compressed="No" TimeStamp="4/26/2010 2:20:11 AM" />
<FileSystem="NTFS" TimeStamp="4/26/2010 2:20:11 AM" />
<TotalSpace="149.05GB (160039239680bytes)" TimeStamp="4/26/2010 2:20:11 AM" />
<AvailableSpace="140.47GB (150827999232bytes)" TimeStamp="4/26/2010 2:20:11 AM" />
<PercentageAvaliable="94.24%" TimeStamp="4/26/2010 2:20:11 AM" />
<VolumeName="OS" TimeStamp="4/26/2010 2:20:11 AM" />
<VolumeSerialNumber="9C3F9B31" TimeStamp="4/26/2010 2:20:11 AM" />
</Disk>
</Disks>

It would be easier to use it.[/QUOTE]

Yes, I agree with you. The only problem is, I cannot change the string as it is GIVEN to my program as an input. I can only work with what I have.

Anyway, I found the solution to my problem which I would like to share with all in this forum for your future reference.

Again the XML string is :



I looked at the XML string again and I did notice that the Param sections consist of Key/Value pairs that need to be captured in some sort of Key/Val data structure ( for instance, Description is a key and "Local Fixed Disk" is the value ).

Hence, I modified the code to look like this ( modified the class to capture the XML data ):

------------------------------------------------------------------------------------------------------



I then modified the LINQ code as follows

---------------------------------------------------------------------------------------------------------