Reputation: 313
I am Loading .xml file with help of XDocument. I succesfully read .xml file.
C# code:
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(),
};
Problem I have is in DataPoint variable. I get only one value in "one" array and all the points are writen in this array. How to divide this data for every readed line?
DataPoint variable now:
"00:00:00, 44004:45:00, 48013:35:00, 60015:00:00, 41519:55:00, 600"
Points data in XML and how I like to have:
"00:00:00, 440
04:45:00, 480
13:35:00, 600
15:00:00, 415
19:55:00, 600"
My XML file:
<blabla>
<Infos>
<ConfigurationName>XXConfigurationName</ConfigurationName>
<DateSaved>14.10.2015 13:14:01</DateSaved>
</Infos>
<Configuration>
<DayRoutine>
<DayRoutine ID="4">
<Button>1</Button>
<SetupOption>StaticBasic_DoffEoff</SetupOption>
<DayRoutinePoints>
<Point0>00:00:00, 440</Point0>
<Point1>04:45:00, 480</Point1>
<Point2>13:35:00, 600</Point2>
<Point3>15:00:00, 415</Point3>
<Point4>19:55:00, 600</Point4>
</DayRoutinePoints>
</DayRoutine>
</DayRoutine>
</Configuration>
</blabla>
Upvotes: 1
Views: 985
Reputation: 236328
Currently you are selecting all DayRoutinePoints
elements of DayRoutine
and that gives you single element. Then you are reading its value which is values of all nested point elements. That's why you have array with single value.
All you need to do - select single DayRoutinePoints
element and get it's child elements:
DataPoints = r.Element("DayRoutinePoints").Elements().Select(c => (string)c).ToList(),
NOTE: With XPath your parsing can look more simple (I omitted also converting points to list)
from r in doci.XPathSelectElements("//Configuration/DayRoutine/DayRoutine[@ID=4]")
select new
{
Button = (string)r.Element("Button"),
DataPoints = from p in r.Element("DayRoutinePoints").Elements()
select (string)p
};
Upvotes: 1
Reputation: 1495
Use this:
DataPoints = String.Join(" ", r.Elements("DayRoutinePoints")
.Elements()
.Select(x=>x.Value.ToString()+Environment.NewLine))
Upvotes: 1
Reputation: 6738
To resolve the selection problem, you need to select all of the descendants of the node <DayRoutinePoints>
and fetch their values.
DataPoints = r.Descendants("DayRoutinePoints")
.Descendants().Select(c => (string)c.Value).ToList(),
The original code was essentially taking the inner text of the DayRoutinePoints
node, which ends up being the content of the node with all the XML stripped out.
Upvotes: 1
Reputation: 26
Try this:
XDocument doci = XDocument.Load(path);
var mijav =
doci.Descendants("Configuration")
.Descendants("DayRoutine")
.Descendants("DayRoutine")
.Where(r => (int) r.Attribute("ID") == 4)
.Select(r => new
{
Button = r.Element("Button").Value,
DataPoints =
r.Elements("DayRoutinePoints").Elements()
.Select(c => (string) c.Value)
.ToList(),
});
Upvotes: 1