Search code examples
c#.netxmlserializationxml-serialization

C# Array of XmlElement is adding another level of nesting


I have the following situation:

  • 4 methods that return XML
  • Have to create one big XML with these 4 XML pieces on an array

A small example of this would be the following:

<garage>
    <owner>daniel</owner>
    <cars>
        <XmlElement>
            <plate>ABC123</plate>
        </XmlElement>
        <XmlElement>
            <plate>DSC563</plate>
        </XmlElement>
        <XmlElement>
            <plate>AIO789</plate>
        </XmlElement>
        <XmlElement>
            <plate>IUE692</plate>
        </XmlElement>
    </cars>
</garage>

I have an array of plate and want to inject that on the car which is an System.Xml.XmlElement[]

The problem is, I can't seem to find a way to get rid of the XmlElement element wrapping my objects, I just wanted it to be like:

<garage>
    <owner>
        <name>daniel</name>
    </owner>
    <cars>
        <plate>ABC123</plate>
        <plate>DSC563</plate>
        <plate>AIO789</plate>
        <plate>IUE692</plate>
    </cars>
</garage>

I've tried playing around with the Xml Attributes but couldn't get exactly what I wanted.

Can someone please give me a hand?

here's the working code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Xunit;
using Xunit.Abstractions;

namespace TestsProjec.XML
{
    public class Tests
    {
        private readonly ITestOutputHelper _testOutputHelper;

        public Tests(ITestOutputHelper testOutputHelper)
        {
            _testOutputHelper = testOutputHelper;
        }

        [Fact]
        public void InjectingXml()
        {
            var serializer = new XmlSerializer(typeof(Garage));
            var cars = new List<XmlElement>();
            cars.Add(StringToXmlElement("<plate>ABC123</plate>"));
            cars.Add(StringToXmlElement("<plate>DSC563</plate>"));
            cars.Add(StringToXmlElement("<plate>AIO789</plate>"));
            cars.Add(StringToXmlElement("<plate>IUE692</plate>"));

            string fullXml;

            var entity = new Garage()
            {
                owner = "Daniel",
                cars = cars.ToArray()
            };

            using (MemoryStream ms = new MemoryStream())
            {
                using (XmlWriter tw = XmlWriter.Create(ms))
                {
                    serializer.Serialize(tw, entity);

                    try
                    {
                        byte[] tmp = new byte[ms.Length - 3];
                        ms.Position = 3; //to skip the UTF-8 preamble
                        ms.Read(tmp, 0, (int)ms.Length - 3);
                        fullXml = Encoding.UTF8.GetString(tmp);
                    }
                    catch
                    {
                        fullXml = null;
                    }
                }
            }

            _testOutputHelper.WriteLine(fullXml);
        }

        public XmlElement StringToXmlElement(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return doc.DocumentElement;  
        }


    }

    public class Garage
    {
        public string owner { get; set; }
        public System.Xml.XmlElement[] cars { get; set; }
    }
}

Solution

  • If you use XElement, instead of XmlElement, you could get output you want, with XmlAnyElement Attribute:

    public class Garage
    {
        public string owner { get; set; }
        [XmlAnyElement]
        public XElement[] cars { get; set; }
    }
    

    Your method StringToXmlElement will look like:

    public XElement StringToXmlElement(string xml)
    {
        return XElement.Parse(xml);
    }