Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlSerializer add attribute

Tags:

c#

.net

winforms

I have this item Class :

public class Movie
{
    public string VideoId { get; set; }
    public string Title { get; set; }
}

And i have List<Movie> of this items and i use this code to Serialize to xml file:

string fileName = index + ".xml";
string serializationFile = Path.Combine(dir, fileName);

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

using (var writer = XmlWriter.Create(serializationFile, settings))
{
    var serializer = new XmlSerializer(typeof(List<Movie>));
    serializer.Serialize(writer, tmpList);
}

And this is the result:

<?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>
    <VideoId>MyId</VideoId>
    <Title>MyTitle</Title>
  </Movie>
  <Movie>
    <VideoId>MyId1</VideoId>
    <Title>MyTitle1</Title>
  </Movie>
  <Movie>
    <VideoId>MyId2</VideoId>
    <Title>MyTitle2</Title>
  </Movie>
  <Movie>
    <VideoId>MyId3</VideoId>
    <Title>MyTitle3</Title>
  </Movie>
</ArrayOfMovie>

And it this possible to add attribute to the ArrayOfMovie node,something like this:

<ArrayOfMovie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" customattribute='Yes'>
like image 583
YosiFZ Avatar asked Oct 02 '22 07:10

YosiFZ


1 Answers

Yes, you can do this using the XmlAttribute attribute. In order to do this, you need to define your custom attribute. It comes with the price of one more class that represents the array (nested in the root node). If you have no problem with this addition, then the solution can look like this:

public class ArrayOfMovie
{
    // define the custom attribute
    [XmlAttribute(AttributeName="CustomAttribute")]
    public String Custom { get; set; }
    // define the collection description
    [XmlArray(ElementName="Items")]
    public List<Movie> Items { get; set; }
}

public class Movie
{
    public string VideoId { get; set; }
    public string Title { get; set; }
}

Then create, fill and serialize as you already do - the one new thing is to fill your custom attribute:

// create and fill the list
var tmpList = new List<Movie>();
tmpList.Add(new Movie { VideoId = "1", Title = "Movie 1" });
tmpList.Add(new Movie { VideoId = "2", Title = "Movie 2" });
// create the collection
var movies = new ArrayOfMovie 
            { 
                Items = tmpList, 
                Custom = "yes" // fill the custom attribute
            };
// serialize
using (var writer = XmlWriter.Create(serializationFile, settings))
{
    var serializer = new XmlSerializer(typeof(ArrayOfMovie));
    serializer.Serialize(writer, movies);
}

The XML output looks like this:

<?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" 
                CustomAttribute="yes">
  <Items>
    <Movie>
      <VideoId>1</VideoId>
      <Title>Movie 1</Title>
    </Movie>
    <Movie>
      <VideoId>2</VideoId>
      <Title>Movie 2</Title>
    </Movie>
  </Items>
</ArrayOfMovie>
like image 50
keenthinker Avatar answered Oct 13 '22 10:10

keenthinker