Wednesday, February 20, 2013

Creating XML in Silverlight


Extensible Markup Language. Well that’s full form of XML for those who don’t know. In this post I am going to describe creating 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:

   1: private XDocument CreateXml()
   2: {
   3:    var xml = new XDocument(new XDeclaration( "1.0", "utf-8", "yes"));
   4:    var mainElement = new XElement("Main");
   5:    var subMainElement = new XElement("SubMain");
   6:    var childElement = new XElement("Hello", "Neelma");
   7:    subMainElement.Add(childElement);
   8:    mainElement.Add(subMainElement);
   9:    xml.Add(mainElement);
  10:    return xml;
  11: }


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


   1: <?xml version="1.0" encoding="UTF-8" ?>
   2: <Main>
   3:   <SubMain>
   4:     <Hello>Neelma</Hello>
   5:   </SubMain>
   6: </Main>


The first line of 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 require external entities to be resolved.

Next we are creating XML’s elements (XML tags in other word). As the XML says, we want <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 child element Hello 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 inside SubMain, so subMainElement will add childElement in it, line #8. Then, subMainElement is inside mainElement, so mainElement will 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 which will then be displayed on the screen via textblock. Here is the code of my entire mainpage.cs file.



   1: using System.Windows.Controls;
   2: using System.Xml.Linq;
   3:  
   4: namespace XntensibleMarker
   5: {
   6:     public partial class MainPage : UserControl
   7:     {
   8:         public MainPage()
   9:         {
  10:             InitializeComponent();
  11:             XmlBlock.Text = CreateXml().ToString();
  12:         }
  13:  
  14:         private XDocument CreateXml()
  15:         {
  16:             var xml = new XDocument(new XDeclaration( "1.0", "utf-8", "yes"));
  17:             var mainElement = new XElement("Main");
  18:             var subMainElement = new XElement("SubMain");
  19:             var childElement = new XElement("Hello", "Neelma");
  20:             subMainElement.Add(childElement);
  21:             mainElement.Add(subMainElement);
  22:             xml.Add(mainElement);
  23:             return xml;
  24:         }
  25:     }
  26: }


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

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top