0

I am new to Xml and have written the code whcih creates an Xml and Reads it back also. But i want to have some modifications in the Xml structure.

What i don't want is ArrayOfMovie tag, which is coming as the root tab. But when i am writing multiple objects into the Xml it shows an ArrayOfMovie tag. As i have to maintain the structure of the class, the upper tag as Movie, then its details and then other movie. Also please if you tell the code to modify the xml, tell the procedure to read the newly structured xml too.

Here is the code for the scenario:

// Movies class which contains the list of Movie objects
 public class Movies
    {
     public List<Movie> movieList = new List<Movie>();

    }

  //Movie class
  public class Movie
    {

        public string Title
        { get; set; }


        public int Rating
        { get; set; }


        public DateTime ReleaseDate
        { get; set; }

    }

    private void CreateXml_Click(object sender, EventArgs e)
    {
        string filePath = path + textBox_XmlFileName.Text+".xml";

        Movie firstMov = new Movie();
        firstMov.Title = "Shrek";
        firstMov.Rating = 2;
        firstMov.ReleaseDate = DateTime.Now;

        Movie secondMov = new Movie();
        secondMov.Title = "Spider Man";
        secondMov.Rating = 4;
        secondMov.ReleaseDate = DateTime.Now;

        Movies moviesObj = new Movies();
        moviesObj.movieList.Add(firstMov);
        moviesObj.movieList.Add(secondMov);
        List<Movie> movList = new List<Movie>() { firstMov,secondMov};

        XmlHandler.SerializeToXml(moviesObj.movieList, filePath);

    }

// The static class and funcion that creates the xml file 
       public static void SerializeToXml(List<Movie> movies ,string filePath)
       {
          XmlSerializer xls= new XmlSerializer(typeof(List<Movie>)); 

       TextWriter tw = new StreamWriter(filePath);
       xls.Serialize(tw, movies);
       tw.Close();
       }

// It Creates the following output

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMovie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Movie>
    <Title>Shrek</Title>
    <Rating>2</Rating>
    <ReleaseDate>2014-05-25T22:55:17.2811063+05:00</ReleaseDate>
  </Movie>
  <Movie>
    <Title>Spider Man</Title>
    <Rating>4</Rating>
    <ReleaseDate>2014-05-25T22:55:17.2811063+05:00</ReleaseDate>
  </Movie>
</ArrayOfMovie>

// The code for reading the file into objects

  public static List<Movie> DeserializeFromXml(string filePath)
   {
       XmlSerializer deserializer = new XmlSerializer(typeof(List<Movie>));
       TextReader tr = new StreamReader(@filePath);
       List<Movie> movie;
       movie = (List<Movie>)deserializer.Deserialize(tr);
       tr.Close();

       return movie;
   }
5
  • XML documents have a single root element. Trying to create a XML document with multiple root elements will make consumption of such non-conforming XML document a burden. Why would you want to do that? Commented May 25, 2014 at 19:46
  • I want to remove the ArrayOfMovies tag. Rest of the thing created is fine. And also want to reaad it back. Commented May 25, 2014 at 19:52
  • So, why exactly do you want to remove the ArrayOfMovies tag? It just makes deserialization more complicated than necessary. Commented May 25, 2014 at 19:53
  • that is the requirement. i also tried to iterate the list and add each movie in that, but it puts on the namespcaces of new xml every time against the movie tag. Commented May 25, 2014 at 19:55
  • 1
    Your question implies that you (or rather your code) is responsible for both the serialization and the deserialization. I would understand the requirement if you must work with a 3rd-party software that provides or consumes malformed XML - but that doesn't seem to be the case here. Thus, i sincerely can only recommend to challenge this requirement which seems to be based on a bad design decision. Commented May 25, 2014 at 20:00

1 Answer 1

1

you may use the XmlRootAttribute if you want to name your root

XmlSerializer deserializer = new XmlSerializer(typeof(List<Movie>), 
new XmlRootAttribute("YourRoot"));
Sign up to request clarification or add additional context in comments.

1 Comment

but that won't be generic then! I want to remove the ArrayOfMovie tag, and the rest of structure is fine, i would also like to read it back too.

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.