none
LINQ to XML Query Help RRS feed

  • Question

  •     <Processes>
          <Process Name="Process x">
            <SubName>Process 11</SubName>
            <ActiveTimeInSeconds>100</ActiveTimeInSeconds>
    	<SubName>Process 12</SubName>
            <ActiveTimeInSeconds>120</ActiveTimeInSeconds>
          </Process>
          <Process Name="Process y">
            <SubName>Process 21</SubName>
            <ActiveTimeInSeconds>210</ActiveTimeInSeconds>
    	<SubName>Process 22</SubName>
            <ActiveTimeInSeconds>220</ActiveTimeInSeconds>
          </Process>
        </Processes>
     

    Hi There,

    I would Like to know how to calculate the Sum of <ActiveTimeInSeconds> thro LINQ to XML.

    Please help me out with the query in C#, I need the Sum of Each process in a Array

    Thx in Advance


    • Edited by Raghul R Tuesday, April 2, 2013 11:36 AM language
    Tuesday, April 2, 2013 11:35 AM

Answers

  • Hi,

    It should looks something like this:

    string xml = @"<Processes> <Process Name='Process x'> <SubName>Process 11</SubName> <ActiveTimeInSeconds>100</ActiveTimeInSeconds> <SubName>Process 12</SubName> <ActiveTimeInSeconds>120</ActiveTimeInSeconds> </Process> <Process Name='Process y'> <SubName>Process 21</SubName> <ActiveTimeInSeconds>210</ActiveTimeInSeconds> <SubName>Process 22</SubName> <ActiveTimeInSeconds>220</ActiveTimeInSeconds> </Process> </Processes>";

    var xmlProcesses = XElement.Parse(xml); var array = (from process in xmlProcesses.Elements("Process") select process.Elements("ActiveTimeInSeconds").Sum(sec => (int)sec)).ToArray();


    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.


    • Edited by JA Reyes Tuesday, April 2, 2013 12:06 PM Syntax
    • Marked as answer by Raghul R Wednesday, April 3, 2013 5:19 AM
    Tuesday, April 2, 2013 12:01 PM

All replies

  • Hi,

    It should looks something like this:

    string xml = @"<Processes> <Process Name='Process x'> <SubName>Process 11</SubName> <ActiveTimeInSeconds>100</ActiveTimeInSeconds> <SubName>Process 12</SubName> <ActiveTimeInSeconds>120</ActiveTimeInSeconds> </Process> <Process Name='Process y'> <SubName>Process 21</SubName> <ActiveTimeInSeconds>210</ActiveTimeInSeconds> <SubName>Process 22</SubName> <ActiveTimeInSeconds>220</ActiveTimeInSeconds> </Process> </Processes>";

    var xmlProcesses = XElement.Parse(xml); var array = (from process in xmlProcesses.Elements("Process") select process.Elements("ActiveTimeInSeconds").Sum(sec => (int)sec)).ToArray();


    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.


    • Edited by JA Reyes Tuesday, April 2, 2013 12:06 PM Syntax
    • Marked as answer by Raghul R Wednesday, April 3, 2013 5:19 AM
    Tuesday, April 2, 2013 12:01 PM
  •   Ragu : Hope the below options helps as a start

    // Option 1 : Gives Process Name from Attribute / Sum based on Process Name XElement objDoc = XElement.Load(@"C:\Sites\stringEx\process.xml"); var query = objDoc.Elements("Process").Where(x => x.Attribute("Name").Value != "").ToList(); foreach (var node in query) { Console.WriteLine("{0} Sum = {1}", node.Attribute("Name").Value, node.Elements("ActiveTimeInSeconds").Sum(x => (int)x)); } //Option 2 : Gives the entire Sum of ActiveTimeInSeconds Console.WriteLine("Sum = {0}",objDoc.Elements("Process").Elements("ActiveTimeInSeconds").Sum(x => (int)x));

    // Option 3 : Gives Process Name / Values as dictionary
               Dictionary <string,string> result = new Dictionary<string,string>();
                 query = objDoc.Descendants("SubName");
               result = query.ToDictionary(x => x.Value, x => x.ElementsAfterSelf().First().Value);
               foreach (var x in result)
                {
                    Console.WriteLine("SubName = {0}  Value = {1} ",x.Key,x.Value);
                }



    • Edited by Venkat786 Tuesday, April 2, 2013 4:03 PM
    Tuesday, April 2, 2013 1:15 PM