Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize a List<T> into XML? [closed]

Tags:

How to convert this list:

List<int> Branches = new List<int>();
Branches.Add(1);
Branches.Add(2);
Branches.Add(3);

into this XML:

<Branches>
    <branch id="1" />
    <branch id="2" />
    <branch id="3" />
</Branches>
like image 741
Ujjwal27 Avatar asked Jun 11 '13 12:06

Ujjwal27


People also ask

Can you serialize a list?

A List can be serialized—here we serialize (to a file) a List of objects. Serialize notes. The next time the program runs, we get this List straight from the disk. We see an example of BinaryFormatter and its Serialize methods.

What is the correct way of using XML serialization?

XML Serialization Considerations Type identity and assembly information are not included. Only public properties and fields can be serialized. Properties must have public accessors (get and set methods). If you must serialize non-public data, use the DataContractSerializer class rather than XML serialization.

What does it mean to serialize XML?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.


2 Answers

You can try this using LINQ:

List<int> Branches = new List<int>();
Branches.Add(1);
Branches.Add(2);
Branches.Add(3);

XElement xmlElements = new XElement("Branches", Branches.Select(i => new XElement("branch", i)));
System.Console.Write(xmlElements);
System.Console.Read();

Output:

<Branches>
  <branch>1</branch>
  <branch>2</branch>
  <branch>3</branch>
</Branches>

Forgot to mention: you need to include using System.Xml.Linq; namespace.

EDIT:

XElement xmlElements = new XElement("Branches", Branches.Select(i => new XElement("branch", new XAttribute("id", i))));

output:

<Branches>
  <branch id="1" />
  <branch id="2" />
  <branch id="3" />
</Branches>
like image 50
Praveen Avatar answered Oct 27 '22 16:10

Praveen


You can use Linq-to-XML

List<int> Branches = new List<int>();
Branches.Add(1);
Branches.Add(2);
Branches.Add(3);

var branchesXml = Branches.Select(i => new XElement("branch",
                                                    new XAttribute("id", i)));
var bodyXml = new XElement("Branches", branchesXml);
System.Console.Write(bodyXml);

Or create the appropriate class structure and use XML Serialization.

[XmlType(Name = "branch")]
public class Branch
{
    [XmlAttribute(Name = "id")]
    public int Id { get; set; }
}

var branches = new List<Branch>();
branches.Add(new Branch { Id = 1 });
branches.Add(new Branch { Id = 2 });
branches.Add(new Branch { Id = 3 });

// Define the root element to avoid ArrayOfBranch
var serializer = new XmlSerializer(typeof(List<Branch>),
                                   new XmlRootAttribute("Branches"));
using(var stream = new StringWriter())
{
    serializer.Serialize(stream, branches);
    System.Console.Write(stream.ToString());
}
like image 21
Dustin Kingen Avatar answered Oct 27 '22 17:10

Dustin Kingen