0

I want to deserialize xml into c#. But I am getting error and cannot fix it.

Error is:

 "There is an error in XML document (1, 2)." 
 ...
 "<A xmlns=''> was not expected."

Deserialize code:

    public static object XmlDeserializeFromString(string objectData, Type type)
    {
        var xmlAttributes = new XmlAttributes();
        var xmlAttributeOverrides = new XmlAttributeOverrides();

        xmlAttributes.Xmlns = false;
        xmlAttributeOverrides.Add(typeof(A10), xmlAttributes);


        var serializer = new XmlSerializer(type, xmlAttributeOverrides);
        object result=null;

        try
        {
            using (TextReader reader = new StringReader(objectData))
            {
                result = serializer.Deserialize(reader);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

        return result;
    }

The class is:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "A-1.0", Namespace = "")]
[System.Xml.Serialization.XmlRootAttribute("A", Namespace = "", IsNullable = false)]  
public  class A10
{

    private string versField;

    private string typeField;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }
}

And testing by:

   string xml = "<A vers=\"1.0\" type=\"pd\">";

   A10 a = (A10) XmlDeserializeFromString(xml, typeof(A10));

The exception is thrown on line:

 result = serializer.Deserialize(reader);

Why this is happening? How to solve this?

6
  • Can you include a snippet of your XML? Commented Oct 1, 2013 at 8:13
  • <A vers="1.0" type="pd"> Commented Oct 1, 2013 at 8:19
  • have you tried using only [System.Serializable()] above your class ? Commented Oct 1, 2013 at 8:19
  • Yes, I have tried. The error is still showing. Commented Oct 1, 2013 at 8:23
  • As an additional point, unless you're really performance limited I would reccomend using the System.Xml.Linq namespace for parsing XML. It comes with XDocument, XElement, and XAttribute. Each of which are generally considered to be easier to use when parsing XML. Commented Oct 1, 2013 at 8:39

1 Answer 1

4

<A vers="1.0" type="pd"> is not XML. See the specs. Your root element is missing its closing tag.

Make your document look like this:

<A vers="1.0" type="pd"></A>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.