dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Please Help: Reading XML attributes via LINQ

  1. #1
    Join Date
    May 2010
    Posts
    7

    Question Please Help: Reading XML attributes via LINQ

    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.

    I have the following string :

    theString = @"

    <MgmtSystemInfo>
    <SystemInfo>
    <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 and store the attributes read in the fields of an object.

    For instance, i want to be able to read the Description and TotalSpace attribute in these sections of the above XML (between the <Disks> </Disks> section of the XML ) :


    <Param Description="Local Fixed Disk" TimeStamp="4/26/2010 2:20:11 AM" />
    <Param TotalSpace="149.05GB (160039239680bytes)" TimeStamp="4/26/2010 2:20:11 AM" />


    How do I do this ?

    I tried this snippet as an example but it does not work ( I get an exception )

    public class DiskDrives
    {
    public string Drive { get; set; }

    public string Description { get; set; }
    }

    XDocument xdoc = XDocument.Parse(sftData);

    List<DiskDrives> LocalDisks =
    (from drives in xdoc.Descendants("Disks")
    select new DiskDrives
    {
    Drive = drives.Attribute("Drive").Value, // This line works and I get "C:" as the value

    // Note the line below causes an exception, if I comment it out, the code works
    Description = drives.Element("Param").Element("Description").Value,

    }).ToList<DiskDrives>();

    foreach (var drives in LocalDisks)
    {
    Console.WriteLine("Drive: " + drives.Drive.ToString());
    Console.WriteLine("Description: " + drives.Description.ToString());
    }

    Your help regarding this matter will be highly appreciated.

  2. #2
    Join Date
    May 2010
    Posts
    7

    Re: Please Help: Reading XML attributes via LINQ

    NOTE TO ALL:

    I changed the above code somewhat and I was able to get the correct values but it isn't quite perfect yet.

    Also, I apologize for also posting this in another thread ( the C-Sharp Programming thread ) as I am not sure which is the more appropriate thread to put this.

    For 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&#37;" 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.

    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" />



    But I wanted to 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...


    I tried this snippet as an example, which works for getting the DESCRIPTION attribute of the PARAM section but not the rezst ( 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>();

    int cnt = 0;

    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.
    Last edited by SeekAndFind; May 3rd, 2010 at 11:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)