Reputation: 482
I have two classes. One of the classes is containing an array of the other class.
I dont want the first Class to be serialized, only the array of the other class, so I am passing the array to the Serialization-Method, but it seems to be loosing its name, as it is later called ArrayOfSecondClass. Can someone help me with this?
Here is some Test-Code describing the Scenario:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace TeaTimeTestEmpty
{
public class FirstClass
{
[XmlArray("RealName")] // Is ignored when SecondClassArray is passed
public SecondClass[] SecondClassArray { get; set; }
}
public class SecondClass
{
public string Name { get; set; }
}
public static class Program
{
public static string SerializeToXml(object objectToSerialize, Type objectType = null)
{
if (objectToSerialize != null)
{
if (objectType == null)
{
objectType = objectToSerialize.GetType();
}
XmlSerializer serializer = new XmlSerializer(objectType);
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, objectToSerialize, null);
string xmlString = "";
foreach (byte currentByte in stream.ToArray())
{
xmlString += (char)currentByte;
}
return xmlString;
}
}
return null;
}
static void Main(string[] args)
{
List<SecondClass> listOfSecondClasses = new List<SecondClass>();
for (int i = 0; i < 10; ++i)
{
listOfSecondClasses.Add(new SecondClass() { Name = "Bob" + i });
}
FirstClass firstClass = new FirstClass() { SecondClassArray = listOfSecondClasses.ToArray()};
// Note that I am passing only the SecondClassArray, not the whole Element
string xml = SerializeToXml(firstClass.SecondClassArray);
}
}
}
Now when I am debugging this, I get the following XML-Code in the variable xml:
<?xml version="1.0"?>
<ArrayOfSecondClass
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SecondClass>
<Name>Bob0</Name>
</SecondClass>
<SecondClass>
<Name>Bob1</Name>
</SecondClass>
<SecondClass>
<Name>Bob2</Name>
</SecondClass>
<SecondClass>
<Name>Bob3</Name>
</SecondClass>
<SecondClass>
<Name>Bob4</Name>
</SecondClass>
<SecondClass>
<Name>Bob5</Name>
</SecondClass>
<SecondClass>
<Name>Bob6</Name>
</SecondClass>
<SecondClass>
<Name>Bob7</Name>
</SecondClass>
<SecondClass>
<Name>Bob8</Name>
</SecondClass>
<SecondClass>
<Name>Bob9</Name>
</SecondClass>
</ArrayOfSecondClass>
My problem now is, that the Name I gave it in FirstClass is lost, and I cant seem to find a way to get it back, not even if I am giving the SecondClass XmlRoot or other tags, it is always calling it ArrayOfSecondClass instead of the wanted name.
I would appreciate it if you could give me a solution to how to give it the name I want it to have.
Upvotes: 1
Views: 70
Reputation: 4046
static void Main(string[] args)
{
List<SecondClass> listOfSecondClasses = new List<SecondClass>();
for (int i = 0; i < 10; ++i)
{
listOfSecondClasses.Add(new SecondClass() { Name = "Bob" + i });
}
FirstClass firstClass = new FirstClass() { SecondClassArray = listOfSecondClasses.ToArray() };
// Note that I am passing only the SecondClassArray, not the whole Element
string xml = SerializeToXml(firstClass.SecondClassArray,"RealNames");
Console.WriteLine(xml);
Console.ReadLine();
}
public static string SerializeToXml<T>(T objectToSerialize, string RootNodeName)
{
if (objectToSerialize != null)
{
XmlRootAttribute root = new XmlRootAttribute(RootNodeName);
XmlSerializer serializer = new XmlSerializer(typeof(T),root);
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, objectToSerialize, null);
string xmlString = "";
foreach (byte currentByte in stream.ToArray())
{
xmlString += (char)currentByte;
}
return xmlString;
}
}
return null;
}
Upvotes: 1
Reputation: 174319
The XmlArray
attribute is part of FirstClass
not of its property, so it is clear that it is not used when the serializer never sees an instance of FirstClass
.
You will need to work with another constructor of the XmlSerializer
class.
This constructor allows you to pass the name of the root element:
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "RealName";
XmlSerializer serializer = new XmlSerializer(objectType, xRoot);
Upvotes: 1