0

I am trying to generate XML like the below based on my class definition:

Final Expected XML:

<images>
    <image id="100">
        <url>www.abc.com</url>
    </image>
    <image id="200">
        <url>www.pqr.com</url>
    </image>
    <image id="300">
        <url>www.ooo.com</url>
    </image>
    <image id="400">
        <url>www.ktr.com</url>
    </image>
</images>

But I am getting below XML based on my class definition:

Incorrect XML:

 <images>
      <image>
        <image>
          <id>100</id>
          <url>www.abc.com</url>
        </image>
        <image>
          <id>200</id>
          <url>www.pqr.com</url>
        </image>
        <image>
          <id>300</id>
          <url>www.ooo.com</url>
        </image>
      </image>
 </images>

There are 2 problems in above XML :

1. Repetitive "Image" tag which does not match with my Final Expected XML structure.

enter image description here

  1. <id> tag should be with <image> tag like this : <image id="1"> but instead it is generating inside tag as below :

enter image description here

Class definition :

public class property : root
{
   public string languages { get; set; } 
   public images images { get; set; }
}

public class images
{
    public List<image> image { get; set; }
}

public class image
{
    public int id { get; set; }
    public string url { get; set; }
}

public root GenerateRootObject(List<Product> products)
{
    root root = new root
    {
        property = new List<property>()
        {
            
        }
    };
    foreach (var product in products)
    {
        var property = new property
        {
            images = GenerateImageTag(product.ImageGalleries);
        };
        root.property.Add(property);
    }
}

public images GenerateImageTag(ICollection<ImageGallery> imagesFromDatabase)
{
    var imageObj = new images();
    imageObj.image = new List<image>();
    foreach (var img in imagesFromDatabase)
    {
        imageObj.image.Add(new image
        {
            id = img.Id,
            url = img.RelativeUrl
        });
    }
    return imageObj;
}

Can someone please help me to fix this structure?

I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • 1. I think you should ditch the `images` **class** and make the Property a `List` directly. – Fildor May 12 '22 at 16:09
  • 2. For `id` to be serialized as an attribute, if I remember correctly, you can use the `[XmlAttribute]` Attribute. – Fildor May 12 '22 at 16:10
  • @Fildor So I should remove the "id" property from the "image" class? – I Love Stackoverflow May 12 '22 at 16:12
  • 1
    3. By the way: I would advise to use proper C# Naming convention in the classes and configre XML-Naming by using [XmlElement( ... )] Attributes. – Fildor May 12 '22 at 16:12
  • 1
    _"So I should remove the "id" property from the "image" class?"_ - No. Just add above it : `[XmlAttribute]` - you might need to add a using statement. – Fildor May 12 '22 at 16:13
  • 1
    And make `public images images { get; set; }` => `public List images { get; set; }` and throw away the `images` class. – Fildor May 12 '22 at 16:15
  • Have a look here: https://dotnetfiddle.net/e1RVlX – Fildor May 12 '22 at 16:34
  • 1
    @Fildor Wow, thank you so much. It really works whatever you have suggested. You can post an answer and I can mark it. Once again thank you so much :) – I Love Stackoverflow May 12 '22 at 17:28
  • 1
    I cannot, Alexei closed the question as duplicate, which is OK for me. Glad I could help you. – Fildor May 13 '22 at 08:28

0 Answers0