我有這個xml,我想反序列化,但問題是我無法在每一天都得到所有的<food>
元素。有沒有辦法通過LINQ由linq反序列化xml,默認有很多元素
<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>
反序列化,並具有這些類
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();
}
而且有這樣的代碼從XmlDocument
反序列化數據的類,但它似乎沒有工作
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
})
你爲什麼以這種方式反序列化?那麼'XmlSerializer'呢? – 2014-11-24 19:03:32
cus xml在URl鏈接上,我想在每個元素分配給它之前檢查和修改每個元素。 – 2014-11-24 19:06:09
在你的setter中驗證它們。 – 2014-11-24 19:09:59