Ok let say i have the following xml file

<Users>
  <User>
    <Name>David</Name>
    <Attempts>
      <Place>Paris</Paris
      <Date>3/29/2012</Date>
      <Duration>50 seconds</Duration>
      <Distance>100</Distance>
    </Attempts>
    <Attempts>
      <Place>New York</Paris
      <Date>7/28/2012</Date>
      <Duration>30 seconds</Duration>
      <Distance>100</Distance>
    </Attempts>
   <Attempts>
      <Place>Paris</Paris
      <Date>8/19/2012</Date>
      <Duration>70 seconds</Duration>
      <Distance>60</Distance>
    </Attempts>
    <Attempts>
      <Place>New York</Paris
      <Date>9/29/2012</Date>
      <Duration>60 seconds</Duration>
      <Distance>200</Distance>
    </Attempts>  
  </User>
  <User>
    <Name>Lenny</Name>
     <Attempts>
      <Place>Paris</Paris
      <Date>9/29/2012</Date>
      <Duration>51 seconds</Duration>
      <Distance>130</Distance>
    </Attempts>
  </User>
</Users>

So what im trying to find is particular data between 2 time frames depending on what user i chose from a drop down list

Let Say i chose Daniel and i chose a timeframe from a starttextbox to be 2/20/12 and a endtextbox to be 9/30/12 So what i want is a listbox that will display the following

User: Daniel

Information from 2/20/12 to 9/30/12

Average Distance from Paris:80

Average Distance form New York:150

Average Duration from Paris:60 seconds

Average Duration from New York:45 seconds

Total Duration:105 seconds

Total Distance:230

Is this possible to do? If so i need help to do so, any help would be greatly appreciated

Recommended Answers

All 4 Replies

You can always give LINQ to XML a try. Another options is to read the XML file with the System.Xml.Linq.XDocument.Load() or System.Xml.XmlDocument.Load() method, locate the User node, and iterate through each of the Attempts nodes (checking dates and adding the Distance, Duration and number of nodes per Place). Give it a go, and post here again if you have any specific issues.

As I can see, the XML file is well formated.

var source = XElement.Load(MyFile)

var Query = from User in source.Descendents("User")

             where source.Element("Name").Value > "David"

             select new {Id = (int32)User.Element("Name").Value,

                              title = (string)User.Element("Attempts").Element("Date").Value}
commented: Please don't give out answers, especially wrong ones, to people who have not shown any attempt yet. -1

@jinus Please don't give out answers, especially wrong ones, to people who have not shown any attempt yet.

As I can see, the XML file is well formated.

No, it isn't (note </Paris instead of </Place>), and neither is your code.

var source = XElement.Load(MyFile)

Where's the semicolon?

var Query = from User in source.Descendents("User")

Descendents() doesn't exist; Descendants() does however.

where source.Element("Name").Value > "David"

Shouldn't we be referring to the User, not the source... again.

select new {Id = (int32)User.Element("Name").Value,

int32 doesn't exist... C# is case-sensitive, eh? Also, casting does not convert between strings and integers. And since when was Name an integer.

title = (string)User.Element("Attempts").Element("Date").Value}

Casting a string to a string... seems unnecessary, but where's the semicolon?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.