0

I need to serialize an class related to this:

public class Root {
    public string[] Elements {get;set;}
}

to an XML like this:

<?xml version="1.0" encoding="utf-8"?>
<Root>
    <Element_01>1st element</Element_01>
    <Element_02>2nd element</Element_02>
    <Element_03>3rd element</Element_03>
    <Element_04>4th element</Element_04>
</Root>

when the object is instantiated like so:

var root = new Root {
    Elements = new[] {
        "1st element", "2nd element", "3rd element"
        "4th element"
    }
};

using the System.Xml.Serialization.XmlSerializer. I have to do it the other way round, too.

Is there any way to achieve this?

fboers
  • 126
  • 8
  • 1
    I don´t think you can do this with the `XmlSerializer`. However I can´t see why you think you need this. You can just serialize the list using `XmlArrayItemAttribute`. This will then serialize to `......`. – MakePeaceGreatAgain Apr 11 '18 at 12:49
  • @HimBromBeere Thanks but this won't help me because the XML is related to an API and has to look exactly like the one above. – fboers Apr 11 '18 at 12:52
  • The only way to achieve is by implementing `IXmlSerializable`. – MakePeaceGreatAgain Apr 11 '18 at 12:53
  • 1
    Do you have to use a serializer or can you also use something like `XDocument`? – Sebastian Hofmann Apr 11 '18 at 12:57
  • @HimBromBeere I'm afraid as well but I hope there is another way... – fboers Apr 11 '18 at 12:58
  • Looks like a duplicate (or near duplicate) of [How do you deserialize XML with dynamic element names?](https://stackoverflow.com/q/37255149/3744182). Do the answers there meet your requirements? – dbc Apr 12 '18 at 22:23

1 Answers1

1

You may want to explore an alternative way using XLinq, for your particular scenario it would be simpler and easier, take a look to how your Root class may be rewritten:

public class Root
{
    public string[] Elements { get; set; }

    public string GetXmlString()
    {
        var rootElement = new XElement("Root");
        for (var i = 0; i < Elements.Length; i++)
        {
            var tag = $"Element_{(i + 1).ToString().PadLeft(2, '0')}";
            var xElement = new XElement(tag, Elements[i]);
            rootElement.Add(xElement);
        }

        return rootElement.ToString();
    }

    public static Root DeserializeXmlString(string xmlString)
    {
        var rootElement = XElement.Parse(xmlString);
        var elementsArray = rootElement
            .Elements()
            .Select(xElement => xElement.Value)
            .ToArray();

        return new Root {Elements = elementsArray};
    }
}

And testing:

private static void Main()
        {
            var root = new Root
            {
                Elements = new[]
                {
                    "1st element", "2nd element", "3rd element", "4th element"
                }
            };
            var xmlString = root.GetXmlString();
            Console.WriteLine(xmlString);

            var deserializedRoot = Root.DeserializeXmlString(xmlString);
            foreach (var element in deserializedRoot.Elements)
                Console.WriteLine(element);

            Console.ReadLine();
        }

Result: enter image description here

You have only to add error validation code and you are pretty much done. For more info on LINQ to XML check this

taquion
  • 2,667
  • 2
  • 18
  • 29
  • Thanks for your reply. I was hoping for a more generic solution because the structure contains many elements like the one in my example. Also the XML is in reality far more complex. So I try to avoid generating the XML by hand. – fboers Apr 12 '18 at 07:19