Farrukh Waheed
Farrukh Waheed

Reputation: 2193

C#: XML Deserialize limitation

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?

Upvotes: 2

Views: 296

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062855

[XmlRoot("foodplaces")]
public class ThisCanBeAnything {...}

There are a range of attributes that impact this, including [XmlRoot(...)], [XmlElement(...)], [XmlAttribute(...)], [XmlArrayItem(...)], [XmlArray(...)], [XmlInclude(...)], [XmlIgnore(...)], etc. They also allow full control of xml namespaces (rather than just the default namespace).

See: https://msdn.microsoft.com/en-us/library/83y7df3e(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.110).aspx for more info.

Note that [Serializable] does not impact xml serialization; don't believe anyone who tells you to include that when you are using XmlSerializer.

Upvotes: 2

Related Questions