XML сериализует несколько элементов с одинаковым именем без пространства имен - PullRequest
0 голосов
/ 09 ноября 2018

У меня есть следующий класс, который я пытаюсь сериализовать. И у меня есть два массива с одинаковым типом данных. Я знаю, что если нам нужно более одного элемента с одинаковыми именами, пространство имен будет другим. Но есть обходной путь для полного удаления этих пространств имен.

[XmlRoot]
public class Album
{
    public string Title { get; set; }
    public string Description { get; set;}
    public int CoverImgIndx { get; set; }
    [XmlElement(ElementName ="Element", Namespace ="www.image.com")]
    public Image[] Images { get; set; }
    [XmlElement(ElementName ="Element", Namespace ="www.cover.com")]
    public Image[] Cover { get; set; }

}

public class Image
{
    public int indx { get; set; }
    public string filepath { get; set; }
}

И я использую XmlSerializer для сериализации этого.

public class Program
{
    public static void Main()
    {
        var album = new Album
        {
            Title = "Album Title",
            Description = "Some explanation.",
            CoverImgIndx = 2,
            Images = new Image[] {
                new Image { indx = 0, filepath = @"C:\Images\file1.jpg" },
                new Image { indx = 1, filepath = @"C:\Images\file2.png" },
                new Image { indx = 2, filepath = @"C:\Images\file3.jpg" }
            },
            Cover =  new Image[] {
                new Image { indx = 0, filepath = @"C:\Images\cover1.jpg" }
            }
        };

        XmlSerializer serializer = new XmlSerializer(typeof(Album));
        serializer.Serialize(Console.Out, album);
    }
}

На выходе я получаю пространство имен для элемента изображения. Есть ли способ удалить пространство имен без необходимости удалять изображения и обложки с одинаковым именем элемента.

<?xml version="1.0" encoding="utf-16"?>
<Album xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>Album Title</Title>
  <Description>Some explanation.</Description>
  <CoverImgIndx>2</CoverImgIndx>
  <Element xmlns="www.image.com">
    <indx>0</indx>
    <filepath>C:\Images\file1.jpg</filepath>
  </Element>
  <Element xmlns="www.image.com">
    <indx>1</indx>
    <filepath>C:\Images\file2.png</filepath>
  </Element>
  <Element xmlns="www.image.com">
    <indx>2</indx>
    <filepath>C:\Images\file3.jpg</filepath>
  </Element>
  <Element xmlns="www.cover.com">
    <indx>0</indx>
    <filepath>C:\Images\cover1.jpg</filepath>
  </Element>
</Album>

Вывод, который я ищу

<?xml version="1.0" encoding="utf-16"?>
<Album xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>Album Title</Title>
  <Description>Some explanation.</Description>
  <CoverImgIndx>2</CoverImgIndx>
  <Element>
    <indx>0</indx>
    <filepath>C:\Images\file1.jpg</filepath>
  </Element>
  <Element>
    <indx>1</indx>
    <filepath>C:\Images\file2.png</filepath>
  </Element>
  <Element>
    <indx>2</indx>
    <filepath>C:\Images\file3.jpg</filepath>
  </Element>
  <Element>
    <indx>0</indx>
    <filepath>C:\Images\cover1.jpg</filepath>
  </Element>
</Album>
...