Creating XML Document using Linq in Csharp

Many times I have encountered a need to create some XML structure format on the fly. You know, it’s not for you, someone else is keeping track of the XML format structure, you just need to generate a file real fast according by their rules but you don’t want to mess around with bare string or text files. We want to have at least some kind of assurance that the generated XML is valid. Yea sure, you can use Microsoft XML Document class features and create the nodes one by one using XmlNode and XmlAttribute elements, but if you have a long set of XML structure it’s gonna be pain in the back. Here comes XML.Linq to the rescue!

Solution:

 string SaveXMLLocation = @"C:\directory\XmlDocument.xml";

 var xmlDocument =
 new XElement("documents",
 new XElement("FirstChild",
 new XElement("attributes",
 new XElement("attr", new XAttribute("name", "version"), new XAttribute("value", "ver1.0")),
 new XElement("attr", new XAttribute("name", "type"), new XAttribute("value", "XML")),
 new XElement("attr", new XAttribute("name", "origin"), new XAttribute("value", "WEB")),
 new XElement("attr", new XAttribute("name", "created"), new XAttribute("value", DateTime.Now.ToString("yyyyMMdd-HHmmss"))),
 new XElement("attr", new XAttribute("name", "ID"), new XAttribute("value", "32154987")),
 new XElement("attr", new XAttribute("name", "RegNumber"), new XAttribute("value", "")),
 new XElement("attr", new XAttribute("name", "enhed"), new XAttribute("value", "")),
 new XElement("attr", new XAttribute("name", "date"), new XAttribute("value", DateTime.Now.ToString("yyyyMMdd"))),
 new XElement("attr", new XAttribute("name", "report"), new XAttribute("value", "Legitimation")),
 new XElement("attr", new XAttribute("name", "LegitimationType"), new XAttribute("value", "Passport")),
 new XElement("attr", new XAttribute("name", "title"), new XAttribute("value", "XML Document ")),
 new XElement("attr", new XAttribute("name", "dokumentformat"), new XAttribute("value", "xml")),
 new XElement("attr", new XAttribute("name", "extension"), new XAttribute("value", ".xml")),
 new XElement("attr", new XAttribute("name", "charset"), new XAttribute("value", "UTF-8")),
 new XElement("attr", new XAttribute("name", "CountryCode"), new XAttribute("value", "MKD")),
 new XElement("attr", new XAttribute("name", "additional atribute"), new XAttribute("value", "someadditionalatribute"))
 ),
 new XElement("content", new XAttribute("filename", "SomethingSomething.xml"), 
 "Some content inside of the XElement"
 ),
 new XElement("base", 
 "All of your base belogns to us"
 )

 )
 );

 xmlDocument.Save(SaveXMLLocation);

Output:


<?xml version="1.0" encoding="utf-8"?>
<documents>
  <FirstChild>
    <attributes>
      <attr name="version" value="ver1.0" />
      <attr name="type" value="XML" />
      <attr name="origin" value="WEB" />
      <attr name="created" value="20161202-164945" />
      <attr name="ID" value="32154987" />
      <attr name="RegNumber" value="" />
      <attr name="enhed" value="" />
      <attr name="date" value="20161202" />
      <attr name="report" value="Legitimation" />
      <attr name="LegitimationType" value="Passport" />
      <attr name="title" value="XML Document " />
      <attr name="dokumentformat" value="xml" />
      <attr name="extension" value=".xml" />
      <attr name="charset" value="UTF-8" />
      <attr name="CountryCode" value="MKD" />
      <attr name="additional atribute" value="someadditionalatribute" />
    </attributes>
    <content filename="SomethingSomething.xml">Some content inside of the XElement</content>
    <base>All of your base belogns to us</base>
  </FirstChild>
</documents>

What you may be forgetting:

1.Don’t forget to include the Xml.Linq libraries and assemblies at the top of the .cs file

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

2. Make sure you have read write privileges in the specified Saving Location and that the Folder is already created. (Folder name ‘directory‘ for this example in ‘C:\‘)

 

This example was done in VS 2012 MVC4 web application

Leave a Reply

Your email address will not be published. Required fields are marked *