Generating XML Root Node Having Colon-Via Serialization

Recently, I got a requirement to generate an XML on the fly with some defined schema. I know this requirement looks very simple at first sight, but actually, it was not.

The XML which was to be generated was having a very specific format as it was supposed to be the input for some third-party tools. So, it means, it should be fully compliant with the prescribed XML node structure. Just have a look at the below schema.

  1. <sl:RandomWindow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sl="http://www.ShwetaBlogs.com/Window" xsi:schemaLocation="http://www.ShwetaBlogs.com/Window wwRandomWindow.xsd">  
  2.    <Title>First Window</Title>  
  3.    <Border>Single</Border>  
  4. </sl:RandomWindow>  

Below are the classes I created for serialization purposes.

  1. public class RandomWindow    
  2. {    
  3.     [XmlAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace="http://www.w3.org/2001/XMLSchema-instance")]    
  4.     public string schemaLocation {get;set;}    
  5.     
  6.     [XmlElement]    
  7.     public string Title {get;set;}    
  8.     
  9.      [XmlElement]    
  10.      public string Border {get;set;}    
  11. }    
By using the XmlElement and XmlAttribute classes, I was able to generate most of the required parts of the XML, as shown below.
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <RandomWindow xsi:schemaLocation="http://www.ShwetaBlogs.com/Window wwRandomWindow.xsd" xmlns:sl="http://www.ShwetaBlogs.com/Window" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    
  3. <Title>First Window</Title>    
  4. <Border>Single</Border>    
  5. </RandomWindow>    

But the only thing which didn’t come up correctly was the root node that is expected to be in the form <sl:RandomWindow>.

So, in order to achieve the root node with prefix and colon, I updated the code to -

  1. [XmlRoot("sl:RandomWindow")]  
  2. public class RandomWindow {…}  

But alas! It gave me some strange results. It converted the prefix to hexadecimal, as shown below. Then I thought, instead of providing a concrete prefix, let’s add namespace itself.

  1. [XmlRoot("RandomWindow", Namespace = "http://www.ShwetaBlogs.com/Window")]  
  2. public class RandomWindow { ...}  

And my result was a bit closer to what I need, but not the exact one. Now, the issue remaining was the extra prefix in front of each element :(.

In order to resolve this issue, I tried various options provided by various blogs, but no luck. However, after spending hours, I was able to crack it by a "hit and try" formula. To hide namespaces at element node level, I provided the namespace value as empty just like in the code shown below.

  1. [XmlRoot("RandomWindow", Namespace="http://www.ShwetaBlogs.com/Window")]    
  2. public class RandomWindow      
  3. {      
  4.     [XmlAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace="http://www.w3.org/2001/XMLSchema-instance")]      
  5.     public string schemaLocation {get;set;}      
  6.       
  7.     [XmlElement(Namespace=" ")]      
  8.     public string Title {get;set;}      
  9.       
  10.      [XmlElement(Namespace=" ")]     
  11.      public string Border {get;set;}      
  12. }    

And that did the trick for me. Finally, I was able to achieve the required format.

  1. <?xml version="1.0" encoding="UTF-8"?>      
  2. <sl:RandomWindow xsi:schemaLocation="http://www.ShwetaBlogs.com/Window wwRandomWindow.xsd" xmlns:sl="http://www.ShwetaBlogs.com/Window" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">      
  3. <Title xmlns=" ">First Window</Title>      
  4. <Border xmlns=" ">Single</Border>      
  5. </sl:RandomWindow>     

Although this issue was pretending to be very small, it ate up so much of my time. This is why I thought to share it here in hopes that it would be helpful for you and will save your time.

Happy troubleshooting !!!


Similar Articles