Issue with using XmlSerializer

2850 views xml
3

Here is my C# code:

        public static T Deserialize<T>(string input) where T : class
        {
            System.Xml.Serialization.XmlSerializer ser = 
              new System.Xml.Serialization.XmlSerializer(typeof(T));

            using (StringReader sr = new StringReader(input))
            {
                var test = ser.Deserialize(sr);
                return (T)ser.Deserialize(sr);
            }
        }

And here is my XML

<releaseinfo>
<mediapackagedirectory>C:\\Temp\\Test1\\</mediapackagedirectory>
<revision>A</revision>
<files>
    <file>C:\\Temp\\Test1\\test1.zip</file>
    <file>C:\\Temp\\Test1\\test1.zip</file>
</files>
<target>C:\\Temp\\Target\\</target>
</releaseinfo>

And here is the error I get:

Exception: {"There is an error in XML document (1, 2)."} InnerException: {" was not expected."}

The error I am getting seems to have something to do with namespace even though my xml has no namespaces. It is exactly as is shown.

answered question

Hello, and welcome to stackoverflow. Might you please edit your question to include your complete exception details as text rather than as a screenshot? It's required here not to to use images for this purpose, see Discourage screenshots of code and/or errors and Why not upload images of code on SO when asking a question for why.

I have modified the post above.

1 Answer

0

Assuming you've got some class "Releaseinfo", add the following attribute to the top:

[Serializable, XmlRoot("releaseinfo")]
public partial class Releaseinfo
{
...
}

Here are more details:

XmlRootAttribute Class

ALSO:

  • Q: Why are you deserializing twice? Why not just return (T)ser.Deserialize(sr);? Was the first line just "test code"?

  • Just a side note: please try to use text (vs. screenshots) whenever possible.

posted this

Have an answer?

JD

Please login first before posting an answer.