XML Serialize List with count

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi
Im having trouble trying to figuring out how to get the count of a list inside the list variable Tag <Two> like the below output:
i need the count in there because I use c++ boost Serialization to deserialize the data on another application
<Obj>
<One>hello</One>
<Two>
<count>2</count>
<item>1</item>
<item>2</item>
</Two>
</Obj>
[Serializable]
public class obj
{
public string One;
[XmlArray(ElementName = "Two")]
[XmlArrayItem("item")]
public List<string> Two;

}

byte[] bytestosend;

obj test1 = new obj();
test1.One = "hello";

test1.Two = new List<string>();

test1.Two.Add("1");
test1.Two.Add("1");

// serialize data
var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
var serializer = new XmlSerializer(o.GetType());
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;

using (var stream = new StringWriter())
using (var writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, o, emptyNamepsaces);

System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();

bytestosend = encoding.GetBytes(stream.ToString());
}

the above code would output:
<obj>
<One>hello</One>
<Two>
<item>1</item>
<item>2</item>
</Two>
</obj>
but i need it to be like this:
<Obj>
<One>hello</One>
<Two>
<count>2</count>
<item>1</item>
<item>2</item>
</Two>
</Obj>
thanks

View the full article
 

Similar threads

Back
Top