E
E
etherreals2015-11-22 15:45:29
.NET
etherreals, 2015-11-22 15:45:29

How to set up XML serialization?

Help to configure XML serialization. Throws this exception:

Unhandled exception of type 'System.InvalidOperationException' in System.Xml.dll
Additional information: There is an error in the XML document (2, 2).

I would also be grateful for any comments on the code.
<?xml version="1.0"?>

<ArrayOfMovie xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<Movie>

    <Title>Alien</Title>

    <Year>1979</Year>

    <Rating>8.8</Rating>

    <Director>Ridley Scott</Director>

</Movie>


<Movie>

    <Title>The Matrix</Title>

    <Year>1999</Year>

    <Rating>8.7</Rating>

    <Director>Wachowski Brothers</Director>

</Movie>


<Movie>

    <Title>Inception</Title>

    <Year>2010</Year>

    <Rating>8.8</Rating>

    <Director>Christopher Nolan</Director>

</Movie>


<Movie>

    <Title>Interstellar</Title>

    <Year>2014</Year>

    <Rating>8.7</Rating>

    <Director>Christopher Nolan</Director>

</Movie>

</ArrayOfMovie>

public class XMLSerialization<T> : IXMLSerialization<T>
    {

        public XMLSerialization() { }

        public void ToXml(string filename, T data)
        {
            if (File.Exists(filename)) File.Delete(filename);
            using (var fileIn = new FileStream(filename, FileMode.Create))
            {
                var ser = new XmlSerializer(typeof(T));
                ser.Serialize(fileIn, data);
            }
        }

        public T FromXml(string filename)
        {
            using (var fs = new FileStream(filename, FileMode.Open))
            {
                var ser = new XmlSerializer(typeof(T));
                return (T)ser.Deserialize(fs);
            }
        } 
    }

[Serializable]
    public class Movie : BaseEntity
    {

        public string Title { get; set; }
        public ushort Year { get; set; }
        public double Rating { get; set; }
        public string Director { get; set; }

        public Movie() { }

        public Movie(string title, ushort year, double rating, string director)
        {
            Title = title;
            Year = year;
            Rating = rating;
            Director = director;
        }

        public override string ToString()
        {
            return string.Format("Title: {0}, year: {1}, rating: {2}, director: {3}", Title, Year, Rating, Director);
        }

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Melz, 2015-11-23
@melz

1. XML (2, 2) is almost 80% wrong type conversion. That is, you are trying to deserialize an object into the wrong type. Or type to list
2. No root element.
Try to serialize/deserialize normal.
Write the encoding in XML, encoding="utf-8".
Write attributes (depending on the version) in the class

[XmlElement]
[XmlRoot("...")]
там еще что-то с array есть.

Catch the exception in catch and see what is written there. There are more details sometimes

T
throughtheether, 2015-11-22
@throughtheether

Additional information: There is an error in the XML document (2, 2).

It probably means " - " characters before top-level nodes. If there is no solid justification for their presence, remove them.

E
etherreals, 2015-11-22
@etherreals

In fact, they are not in the code, it just happened to be transferred to the toaster

V
Vyacheslav Zolotov, 2015-11-22
@SZolotov

And if you make a special insertion of XML as C# code? Will serialization work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question