10

I am trying to deserialize the following xml

<?xml version="1.0" encoding="utf-8"?>
<ns2:myroot xmlns:ns2="http://jeson.com/">
  <item>
    <name>uno</name>
    <price>1.25</price>
  </item>
  <item>
    <name>dos</name>
    <price>2.30</price>
  </item>
</ns2:myroot>

with these classes

public class item
{
    [XmlElement(Namespace="")]
    public string name { get; set; }

    [XmlElement(Namespace = "")]
    public double price { get; set; }
}

[XmlRoot("myroot", Namespace="http://jeson.com/")]  //This was http://jeson.com, no slash at the end.
public class myrootNS
{
    [XmlElement(Namespace = "")]
    public item[] item { get; set; }
}

using this method

XmlSerializer serializer = new XmlSerializer(typeof(T), "http://jeson.com/");
XmlReaderSettings settings = new XmlReaderSettings();
using (StringReader textReader = new StringReader(xml))
{
    using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
    {
        return (T)serializer.Deserialize(xmlReader);
    }
}

but somehow I keep getting this error.

System.InvalidOperationException: There is an error in XML document (2, 2). ---> 
System.InvalidOperationException: <myroot xmlns='http://jeson.com/'> was not expected.

What is the correct way of doing it ? The method works for deserializing without the namespace.

1
  • Turns out my namespace did not have slash at the end. As Alex points out, it should have. Dec 8, 2014 at 21:26

2 Answers 2

12

The problem is that the namespace of myrootNS class is incorrect because it doesn't match the expected namespace in the XML.

[XmlRoot("myroot", Namespace = "http://jeson.com/")]
public class myrootNS
{
    [XmlElement(Namespace = "")]
    public item[] item { get; set; }
}

Notice that the Namespace property value has a trailing /. This is my deserialize method:

static T Deserialize<T>(string xml)
{
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    XmlReaderSettings settings = new XmlReaderSettings();
    using (StringReader textReader = new StringReader(xml))
    {
        using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
        {
            return (T)serializer.Deserialize(xmlReader);
        }
    }
}
5
  • your code is fine, just add the / character at the end on the namespace in your myrootNS class, for that match with your xml expected. Dec 8, 2014 at 19:38
  • 1
    In the XmlSerialezer constructor you don't need to specified the namespace, because you did in your myrootNS class. You can put XmlSerializer serializer = new XmlSerializer(typeof(T)); Dec 8, 2014 at 19:42
  • Did you try your suggestion? It broke one mine. Dec 8, 2014 at 19:52
  • Sure, kindly share please :) Dec 8, 2014 at 20:01
  • If it is specified in the XML file, it shouldn't be needed in the XmlRootAttribute. What is the workaround?
    – Suncat2000
    Mar 10, 2020 at 21:39
4

As an alternative to the XmlRoot attribute, you can also use the alternative XmlRootAttribute constructor of XmlSerializer to override when the element name or namespace differ:

var serializer = new XmlSerializer(typeof(myrootNS), 
                     new XmlRootAttribute                             
                     { 
                         ElementName = "myroot", 
                         Namespace = "http://jeson.com/" 
                     });
1
  • 1
    Thank you, turns out specifying custom root name in poco is not enough, I have to specify it in the serializer too. Thank you. Dec 8, 2014 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.