Creating XML in Silverlight

Extensible Markup Language. Well that's full form of XML for those who don't know. In this article I describe creation of a XML document in Silverlight. We don't need a cool user interface for that, just a text block will work. So in a new Silverlight project I have created the following function:

private XDocument CreateXml()
{
    var xml = new XDocument(new XDeclaration( "1.0", "utf-8", "yes"));
    var mainElement = new XElement("Main");
    var subMainElement = new XElement("SubMain");
    var childElement = new XElement("Hello", "Neelma");
    subMainElement.Add(childElement)
    mainElement.Add(subMainElement);
    xml.Add(mainElement);
    return xml;
}

The Class XDocument is in the System.Xml.Linq namespace, for that you need to add the System.Xml.Linq.dll into the project. Once that is in place, let us talk about the function and what it does. As the return type of the function says, the function will return a XmlDocument. Now, we need to have the following XML output:

<?xml version="1.0" encoding="UTF-8" ?>
    <Main>
        <
SubMain>
            <
Hello>Neelma</Hello>
        </SubMain>
    </
Main>

The first line of the function, line #3, will begin creating a new XmlDocument (scribbled as XDocument). It will take three arguments, version, encoding and standalone.

  1. Version specifies the version of XML that is to be used, usually 1.0.

  2. Encoding specifies the encoding of the XML thus generated, usually utf-8.

  3. Standalone is a string containing "yes" or "no" that specifies whether XML is standalone or requires external entities to be resolved.

Next we are creating XML's elements (XML tags in other words). As the XML says, we want the <Main> tag at the top. So we've defined Main first, line #4. Next we want element SubMain, so in line #5 we declared SubMain. Finally we want a child elementHello inside SubMain, in line#6, we've declared that. The XElement class takes two arguments; first, name of the element and second, its value. When value is not specified, the element is considered as an empty tag.

There is no relation on how you declare elements with their position in the code, instead it all depends on how you add them inside each other. To keep it simple and easy to understand, I followed the hierarchy.

Next comes the placing of elements inside each other. Looking through the XML above, the child element Hello is insideSubMain, so subMainElement will add childElement in it, line #8. Then, subMainElement is inside mainElement, so mainElementwill add subMainElement to it, line #9. Finally again, the mainElement is under the entire XML document, so mainElement will be added into the XML. The function in the end will return the XML document that will then be displayed on the screen via textblock. Here is the code of my entire mainpage.cs file.

using System.Windows.Controls;
using System.Xml.Linq;
 
namespace XntensibleMarker
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            XmlBlock.Text = CreateXml().ToString();
        } 
        private XDocument CreateXml()
        {
            var xml = new XDocument(new XDeclaration( "1.0", "utf-8", "yes"));
            var mainElement = new XElement("Main");
            var subMainElement = new XElement("SubMain");
            var childElement = new XElement("Hello", "Neelma");
            subMainElement.Add(childElement);
            mainElement.Add(subMainElement);
            xml.Add(mainElement);
            return xml;
        }
    }
}

Thus, we have created a XML document in Silverlight. You can download the entire solution from http://www.4shared.com/rar/z7x2qd3b/XntensibleMarker.html?


Similar Articles