esispaned
esispaned

Reputation: 313

xml xdocument, read attribute

I want to read XML document with help of XDocument. I already search over the internet for the right solution. I found this example, but didnt work like it should.

My XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<blabla>
<Infos>
<ConfigurationName>XXConfigurationName</ConfigurationName>
<DateSaved>14.10.2015 10:41:54</DateSaved>
</Infos>
<Configuration>

<DayRoutine>
  <DayRoutine ID="4">
    <Button>1</Button>
    <DayRoutinePoints>
      <Point0>00:00:00, 243</Point0>
      <Point1>00:00:00, 243</Point1>
      <Point2>00:00:00, 243</Point2>
      <Point3>00:00:00, 243</Point3>
    </DayRoutinePoints>
  </DayRoutine>

  <DayRoutine ID="3">
    <Button>5</Button>
    <DayRoutinePoints>
      <Point0>00:00:00, 243</Point0>
      <Point1>00:00:00, 243</Point1>
      <Point2>00:00:00, 243</Point2>
      <Point3>00:00:00, 243</Point3>
    </DayRoutinePoints>
  </DayRoutine>
</DayRoutine>

</Configuration>
</blabla>

And my C# code - updated:

XDocument doci = XDocument.Load(path);
        var mijav = from r in doci.Descendants("Configuration").Descendants("DayRoutine").Descendants("DayRoutine").Where(r => (int)r.Attribute("ID") == 4)
                    select new
                    {
                        Button = r.Element("Button").Value,
                        DataPoints = r.Elements("DayRoutinePoints").Select(c => (string)c.Value).ToList(),
                    };

My Current C# code dont give me anything. I like to read data from DayRoutine ID="3". How to do that? Because I try to erase "Where(r => (int)r.Attribute("ID")==3)" and I get result from first "DayRoutine". But I like to have data from "second" DayRoutine.

And later I like to read Points. But because it is not every time the same number of points, how to read this points with some loop?

Thanks for help and please ask if you have any question

Upvotes: 0

Views: 126

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273621

How I can read points now? It can be points from 0 to 60.

This basic approach gives you the points as XElements.

...
new 
{
   ...
   //DataPoints = r.Descendants("DayRoutinePoints").Descendants("Point1"),
     DataPoints = r.Elements("DayRoutinePoints").ToList(),
}

Add a .Select(xe => ...) before the ToList to convert to your own class.

Upvotes: 1

Related Questions