If this is what you want:
<ArrayOfB>
<b>
<a>Name</a>
</b>
<b>
<a>Age</a>
</b>
</ArrayOfB>
Then this should work.
public class XMLPlayground
{
public void Play()
{
List<b> list = new List<b>()
{
new b() {a = "Name"},
new b() {a = "Age"},
};
string str = SerializeToString(list);
Console.WriteLine(str);
}
private string SerializeToString(object o)
{
if (o == null) return "";
var xs = new XmlSerializer(o.GetType());
XmlSerializerNamespaces tellTheSeriliserToIgnoreNameSpaces = new XmlSerializerNamespaces();
tellTheSeriliserToIgnoreNameSpaces.Add(String.Empty, String.Empty);
XmlWriterSettings tellTheWriterToOmitTheXmlDeclaration = new XmlWriterSettings { OmitXmlDeclaration = true };
using (StringWriter writer = new StringWriter())
{
using (var xw = XmlWriter.Create(writer, tellTheWriterToOmitTheXmlDeclaration))
{
xs.Serialize(xw, o, tellTheSeriliserToIgnoreNameSpaces);
return writer.ToString();
}
}
}
}
[Serializable]
public class b
{
public string a { get; set; }
}
Here's some insight on what you want to achieve, but to me the solution looks rather esoteric. You may wanna try SAX approach with XmlTextReader and construct your object on the fly.
C# Xml serialization, collection and root element