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); //*this is the line that breaks*
                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: {"<releaseinfo xmlns=''> 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.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I have modified the post above. – DrunkOldHobo Nov 08 '18 at 03:57
  • "seems to have something to do with namespace" - nah, it is complaining about the full name; the full name is a combination of two things: the local name (`releaseinfo`) and the namespace (which can be via `xmlns=` or via an alias prefix). It is simply *telling* you about both the local name and the effective namespace so that you can understand the context. The problem is almost certainly that the *name* is unexpected – Marc Gravell Nov 08 '18 at 23:30

1 Answers1

3

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.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Yea, I used the "var test" line just to test why the original line was breaking. I thought it had something to do with the way I was declaring the object "T", but apparently not. – DrunkOldHobo Nov 08 '18 at 04:02
  • 1. Thank you for replacing the screen shot with the error text. 2. Did the XmlRoot attribute resolve the problem? 3. Definitely "upvote" the reply if you found any of it helpful. – paulsm4 Nov 08 '18 at 06:38
  • It seems the error has nothing to do with the class. It seems that there is something with the "deseriaize" method. "(T)ser.Deserialize(sr); " It breaks right there, regardless of what I do with the class or object, or even if I don't specify an object and just use "var", as my testline indicated. – DrunkOldHobo Nov 08 '18 at 16:17
  • You will absolutely get the error `" was not expected."` if you don't annotate your class with `[Serializable, XmlRoot()]` ... or equivalent. For example, you could explicitly declare a namespace in your .xml file. But adding `[Serializable, XmlRoot()]` *SHOULD* get you past this specific problem in this particular case. Look here for more XmlSerializer troubleshooting tips: https://msdn.microsoft.com/en-us/library/aa302290.aspx – paulsm4 Nov 08 '18 at 20:20
  • `[Serializable]` is not needed here; `XmlSerializer` doesn't care at all about that pseudo-attribute – Marc Gravell Nov 08 '18 at 23:27