Can't seem to deserialize very simple xml using XmlSerializer in c#

can definition
can synonym
can verb
can noun meaning
can bus
can airport
can network
can band

I feel like I am going made. I have written a hundred deserializing routines, but this one is killing me!

Below is what I get returned from a service. A very simple array of strings...I think.

<ArrayOfstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <string>Action &amp; Adventure</string>
    <string>Comedy</string>
    <string>Drama</string>
    <string>Family</string>
    <string>Horror</string>
    <string>Independent &amp; World</string>
    <string>Romance</string>
    <string>Sci-Fi/Fantasy</string>
    <string>Thriller &amp; Crime</string>
</ArrayOfstring>

I am using out the box deserializing

var serializer = new XmlSerializer(typeof(List<string>));
var reader = new StringReader(xmlString);
var GenreList = (List<string>)serializer.Deserialize(reader);

but I get the following error on the Deserialize line:

<ArrayOfstring xmlns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'> was not expected

I have tried including the namespace and creating all manner of exotic objects in an attempt to get this to work. Crazy amount of time. In the end I have requested it in JSON and deserialised that with Json.net.

However I am curious as to what I have been doing wrong!

Of course XmlSerializer can deserialize it. All you need is to create XmlSerializer as follows

var serializer = new XmlSerializer(typeof(List<string>), 
                                   new XmlRootAttribute() { ElementName = "ArrayOfstring", Namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays" });

Can, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most� Rutgers Canvas is pleased to announce the integration of Flipgraid, a free video discussion platform from Microsoft that helps educators see and hear from every student in class and foster a fun and supportive social learning environment.

The XML Serializer cannot deserialize a simpletype or a list of simple types without additional specification, but the DataContractReader can:

        string content = @"
        <ArrayOfstring xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
            <string>Action &amp; Adventure</string>
            <string>Comedy</string>
            <string>Drama</string>
            <string>Family</string>
            <string>Horror</string>
            <string>Independent &amp; World</string>
            <string>Romance</string>
            <string>Sci-Fi/Fantasy</string>
            <string>Thriller &amp; Crime</string>
        </ArrayOfstring>";

        var serializer = new DataContractSerializer(typeof(string[]));
        var reader = new XmlTextReader(new StringReader(content));
        var GenreList = new List<string>((string[])serializer.ReadObject(reader));

Can, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). We would like to show you a description here but the site won’t allow us.

You can also use a simple class to achieve the same results. Note that I removed the namespaces from your XML file for brevity. You can implement reading of the namespaces in the serializer if you please.

    public class ArrayOfstring
    {
        [XmlElement("string")]
        public List<string> strings;
    }

    private void Deserialize(string xmlString)
    {
        var serializer = new XmlSerializer(typeof(ArrayOfstring));
        var reader = new StringReader(xmlString);
        var GenreList = ((ArrayOfstring) serializer.Deserialize(reader)).strings;
    }

The Official CAN / Spoon Records Website, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. In normal presidential elections, the labors of campaign strategists don't change much. Underlying conditions in the country drive the outcome.

This will work

DataContractSerializer xmlSer = new DataContractSerializer(typeof(string[]));
TextReader reader=new StreamReader(xmlString);
var stringArr= (string[])xmlSer.ReadObject(reader);
List<string> listStr=new List<>();
for(var s in stringArr)
{
listStr.Add(s);
}

can, can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. 50+ videos Play all Mix - CAN'T STOP THE FEELING! (From DreamWorks Animation's "Trolls") (Official Video) YouTube Pharrell Williams - Happy (Official Video) - Duration: 3:55.

I realize this is an old question, but I recently ran on to the same issue and wanted to share what worked for me. I tried all the approaches outlined as potential solutions, but couldn't get any of them to work. Even specifying the namespace in the XmlRootAttribute approach would throw the "was not expected" error reported in the original problem. I was getting the ArrayOfString as a response from an API, so I used an XDocument parse approach:

List<string> lstGenre = new List<string>();
var response = await client.PostAsync(url, content);
var responseString = await response.Content.ReadAsStringAsync();

XDocument xdoc = XDocument.Parse(responseString);
XNamespace ns = xdoc.Root.GetDefaultNamespace();
XElement root = xdoc.Element(XName.Get("ArrayOfString", ns.NamespaceName));
IEnumerable<XElement> list = root.Elements();
foreach (XElement element in list)
{
    string item = element.Value;  // <-- individual strings from the "ArrayOfString"
    lstGenre.Add(item);
}

Can, A can is a metal container in which something such as food, drink, or paint is put. The container is usually sealed to keep the contents fresh. Several young men� Access Canvas, get training, find support, look-up grades, and download apps. Students and faculty use canvas to complete and administer course materials.

CAN, can 2. (kăn). n. 1. A usually cylindrical metal container. 2. The Log In button can be found at the very top of this page. COVID-19 – Preparing for the Road Ahead From venturing out to reopening your business, find tips, offers, and support to help you move forward.

Can definition and meaning, Though I may not be here with you, I urge you to answer the highest calling of your heart and stand up for what you truly believe. Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.

Can, You can customize the map before you print! Click the map and drag to move the map around. Position your mouse over the map and use your mouse-wheel to zoom in or out.

Comments
  • -1 The XML Serializer cannot deserialize a simpletype or a list of simple types I wouldn't speak so sure. (BTW: My downvote is only for the false statement regarding the future readers, If you remove it and explain only DataContractReader, I will undo my downvote)
  • Updated the statement. Without adding a root element, specifying namespaces or other 'tricks' it doesn't.
  • where does one find the DataContractSerializer?
  • msdn.microsoft.com/en-us/library/…
  • I tried this and it still failed. [XmlRoot] public class ArrayOfstring { [XmlElement("string")] public string[] Strings { get; set; } }