Monday, 24 November 2014

deserialize xml by linq, defecault whith many elements



I have this xml that i want to deserialize, but the problem is that i cannot get all the elements in each day. Is there any way to deserialize it by linq



-<foodplaces>


-<foodplace>
<name> The Indian Restaurant</name>
<week> 47 </week>
-<monday>
<food> Pasta </food>
<food> chineese food</food>
<food> veg food </food>
</monday>
-<tuesday>
<food> Indian food</food>
<food> Veg food </food>
</tuesday>
</foodplace>

<name> Restauran Italian </name>
<week> 47 </week>
-<monday>

<food> Pizza </food>
<food> Checken </food>
<food> sallad </food>
</monday>
-<tuesday>
<food> Fish </food>
<food> ris </food>
<food> Biff </food>
<food> Checken </food>
</tuesday>
</foodplaces>


and have these classes



public class tuesday
{
private string[] _foods = new string[3];
public string food1
{
set { _foods[0] = value; }
}
public string food2
{
set { _foods[1] = value; }
}
public string food3
{
set { _foods[2] = value; }
}
}


public class foodplace
{
private string _name;
private string _week;
public string name
{
get { return _name; }
set { _name = value; }
}
public string week
{
get { return _week; }
set { _week = value; }
}
public monday m = new monday();
public tuesday t = new tuesday();
}


And have this code to deserialize data from XmlDocument to the class, But seems not to work



foodplace fd = new foodplace();
List<foodplace> foodplaces =
(from _foodplace in xdocument.Element("foodplaces").Elements("foodplace")
select new foodplace
{
name = _foodplace.Element("name").Value,
week = _foodplace.Element("week").Value,

m = (from _day in _foodplace.Elements("monday")
select new tuesday
{

food1 = _day.Element("food").Value,
food2 = _day.Element("food").Value,

food3 = _day.Element("food").Value

})

No comments:

Post a Comment