There are many different techniques to use by which you can create an XML document in C#. One of them is LINQ to XML which we are going to discuss in this article.
Let’s say we need to create an XML as below:
- <?xml version="1.0" encoding="UTF-8"?>
- <Parent>
- <Header>
- <FileDetails>
- <FileName>RandomFile</FileName>
- <FileVersion>1.0</FileVersion>
- </FileDetails>
- </Header>
- <Body>
- <Infos>
- <Info Type="Information1">This is Information1</Info>
- <Info Type="Information2">This is Information2</Info>
- </Infos>
- <Users>
- <UserDetails>
- <Name>
- <FirstName>Vipul</FirstName>
- <MiddleName/>
- <LastName>Malhotra</LastName>
- </Name>
- <DateOfBirth>12-Apr-1990</DateOfBirth>
- </UserDetails>
- </Users>
- </Body>
We will first create the below Xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <Parent>
- <Header>
- <FileDetails>
- <FileName>RandomFile</FileName>
- <FileVersion>1.0</FileVersion>
- </FileDetails>
- </Header>
- <Body>
- <Infos>
- <Info Type="Information1">This is Information1</Info>
- <Info Type="Information2">This is Information2</Info>
- </Infos>
- </Body>
- XDocument doc = new XDocument(new XElement("Parent"));
Let’s first create the Header portion of the xml.

Please notice that the XElement “Header “ is added as a new element and the further elements are added as nested to this “Header” element. It is due to the reason that the elements are sub-elements of “Header”. Further “FileName” and “FileVersion” element is a sub-element of “FileDetails”
In the same way, we would add another section to the root of the doc. This section would be “Body”.
The code for the same would be as:

This follows the same logic that “Body” is also sub-node of the root “parent” and so it is added directly to the root. Whereas , the element “Infos” is sub-element of “Body” and is so added in the way above. Same goes for “Info” which is a further sub-element of “Infos”.
Also notice how an attribute is added to each of the “Info” element using XAttribute.
After this, we further need to add the below section as sub-nodes of “Body” and not the root of the application:
- <Users>
- <UserDetails>
- <Name>
- <FirstName>Vipul</FirstName>
- <MiddleName/>
- <LastName>Malhotra</LastName>
- </Name>
- <DateOfBirth>12-Apr-1990</DateOfBirth>
- </UserDetails>
- </Users>
Using XDocument, we can search for the node “Body” and then start adding node XElements to it .
Searching a node Is done using:
Further adding more elements to it is done using the below code:

The logic behind the hierarchy is the same as that discussed above.
The code can also be used inside a loop in case we need to add many similar sections to a particular node. Like in this case there can be many users and all of their details would have to be added in different UserDetails section inside the “Body” node.
I am attaching a the code project with this article.
In case of any questions, please reach out to me in the comments below.

pooja guptaPosted May 26, 2016, 2:45 PM
Good
Bhuvanesh MohankumarPosted May 4, 2016, 2:41 PM
Nice