Create XML document from object list : XElement « XML LINQ « C# / C Sharp






Create XML document from object list

   

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Xml.Linq;
class Employee {
    int _id;
    int _idRole;
    string _lastName;
    string _firstName;

    public int ID {
        get { return _id; }
        set { _id = value; }
    }

    public int IDRole {
        get { return _idRole; }
        set { _idRole = value; }
    }

    public string LastName {
        get { return _lastName; }
        set { _lastName = value; }
    }

    public string FirstName {
        get { return _firstName; }
        set { _firstName = value; }
    }
}
class Program {
    static void Main(string[] args) {
        List<Employee> people = new List<Employee> {
               new Employee { ID = 1, IDRole = 1, LastName = "A", FirstName = "B"},
               new Employee { ID = 2, IDRole = 2, LastName = "G", FirstName = "T"}
            };
        XElement xml = new XElement("people",
                                from p in people
                                select new XElement("person",
                                            new XElement("id", p.ID),
                                            new XElement("firstname", p.FirstName),
                                            new XElement("lastname", p.LastName),
                                            new XElement("idrole", p.IDRole)));
        Console.WriteLine(xml);


    }
}

   
    
  








Related examples in the same category

1.Create an XElement class with the specified name.
2.Create an XElement class with the specified name and content.
3.Create an XElement class from an XStreamingElement object.
4.The Elements method returns just the child nodes of type XElement
5.Calling the ToString Method on an Element Produces the XML Tree
6.Immediate Execution of the XML Tree Construction
7.Casting an Element to Its Value's Data Type Outputs the Value
8.Different Node Value Types Retrieved via Casting to the Node Value's Type
9.Casting a Node to a Different Data Type Than Its Value's Original Data Type
10.Creating an Element Using the First Prototype
11.Use XElement.Parse to parse an element document
12.Is an element empty
13.Accessing the XML Document from an XElement Object via the Document Property
14.An Example with a Single Book
15.Adding a Node to the End of the Specified Node's Child Nodes with Add
16.Obtaining Elements Without Reaching
17.Obtaining Restricted Elements Without Reaching
18.Obtaining Restricted Elements Without Reaching While Ordering and Using Query Expression Syntax
19.Suppressing Node Construction with null
20.Generates an Empty Element
21.Prevents an Empty Element
22.Handling Multiple Peer Nodes While Maintaining a Flat Structure
23.XElement.Load("Employee.xml")
24.Creates the XML Tree
25.Saving an Element with the XElement.Save Method
26.Loading an Element with the XElement.Load Method
27.Parsing an XML String into an Element
28.XElement Class Represents an XML element.
29.Use Linq to create XElement
30.Create a new XElement class from another XElement object.
31.XElement.HasElements gets a value indicating whether this element has at least one child element.
32.XElement.IsEmpty gets a value indicating whether this element contains no content.
33.Count the node with whitespace
34.Get line number and line position
35.XElement.Name gets or sets the name of this element.
36.XElement.NodeType gets the node type for this node.
37.XElement.SetElementValue sets the value of a child element, adds a child element, or removes a child element.
38.XElement.SetValue sets the value of this element.
39.XElement.Value gets or sets the concatenated text contents of this element.
40.XElement.WriteTo writes this element to an XmlWriter.
41.XElement to Boolean Cast
42.XElement to DateTime Cast
43.Rename element