I'm kinda newbie with XML serial/deserialize things and tyring to write some Generic classes.
While using XmlSerializer.Deserialize(typeof(T))
, I realize that T object should have the same name as that of the Parent element in my XML. Here is the XML document I'm using for this example i.e. FoodPlaces.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>
<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>
</foodplace>
</foodplaces>
And this is how, I'm deserializing this xml:
var serializer = new XmlSerializer(typeof(foodplaces));
var fs = new FileStream(@"D:\FoodPlaces.xml", FileMode.Open);
var reader = XmlReader.Create(fs);
var fp = (foodplaces)serializer.Deserialize(reader);
fs.Close();
This will work absolutely fine because my storage class name is "foodplaces" which is the topmost/parent element in the XML file.
When I tried to rename my storage class to MyFoodPlaces, this happened:
*System.InvalidOperationException was unhandled
HResult=-2146233079
Message=There is an error in XML document (1, 2).
Source=System.Xml
//Skipping stack trace:
InnerException:
HResult=-2146233079
**Message=<foodplaces xmlns=''> was not expected.**
Source=Microsoft.GeneratedCode*
This would be corrected if I rename it back to "foodplaces" which is the topmost/parent element in the XML.
Questions:
1. What if I want to store this XML data in some other class with different name e..g MyfoodPlaces?
2. Is there any solution in using LinQ?