Tagged: XSD Toggle Comment Threads | Keyboard Shortcuts

  • Ali BaderEddin 10:23 pm on December 29, 2009 Permalink | Reply
    Tags: , deserialize, empty, encoding, generic, Indent, List, namespace, OmitXmlDeclaration, serialize, , xmlserializer, XSD   

    Xml Serialization – Tips & Tricks 


    Let’s say we have an XSD representing a library with a list of books and employees in it. 

    <?xml version="1.0" encoding="utf-8"?>
    
    <xsd:schema id="Lib"
               targetNamespace="http://schemas.ali.com/lib/"
               elementFormDefault="qualified"
               xmlns="http://schemas.ali.com/lib/"
               xmlns:mstns="http://schemas.ali.com/lib/"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               version="1.0"
               attributeFormDefault="unqualified">
    
      <xsd:element name="Library" type="LibraryType" />
    
      <xsd:complexType name="LibraryType">
        <xsd:all>
          <xsd:element name="Books" type="BooksType" minOccurs="0" maxOccurs="1" />
          <xsd:element name="Employees" type="EmployeesType" minOccurs="0" maxOccurs="1" />
        </xsd:all>
      </xsd:complexType>
    
      <xsd:complexType name="BooksType">
        <xsd:sequence minOccurs="0" maxOccurs="unbounded">
          <xsd:element name="Book" type="BookType" />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name="EmployeesType">
        <xsd:sequence minOccurs="0" maxOccurs="unbounded">
          <xsd:element name="Employee" type="EmployeeType" />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name="BookType">
        <xsd:attribute name="Title" type="xsd:string" use="required" />
        <xsd:attribute name="Author" type="xsd:string" use="optional" />
      </xsd:complexType>
    
      <xsd:complexType name="EmployeeType">
        <xsd:attribute name="Name" type="xsd:string" use="required" />
      </xsd:complexType>
    
    </xsd:schema>

    Here is a sample Xml using this XSD: 

    <?xml version="1.0" encoding="utf-8" ?>
    
    <Library xmlns="http://schemas.ali.com/lib/">
      <Books>
        <Book Title="Book 1" Author="Ali"/>
        <Book Title="Book 2" Author="Sara"/>
      </Books>
      <Employees>
        <Employee Name="Ali"/>
        <Employee Name="Sara"/>
      </Employees>
    </Library>

    Tip 1 – Generating Code from XSD 

    We’d like to have an object representation of this Xml. Thus, we’ll use the Xml Schema Definition tool to generate .Net C# code from the XSD, as follows: 

    • Start Visual Studio Command Prompt
    • Run this command: xsd “path to XSD file” -language:CS /classes /outputdir:”path to output directory”

    As a result, a .cs file will be generated and copied to the output directory. Take a look at the file GeneratedLibrary.cs

    Tip 2 – Using List<T> instead of array 

    The <Books> and <Employees> elements are generated as arrays. So I would like to change those arrays to List<T> objects to make it easier to add items to them instead of having to worry about the size of the array and expanding it. Take a look at the modified class LibraryWithLists.cs. However, this is still not good enough because if I want to create a library with one book, I’ll have to write the following code: 

    LibraryType library = new LibraryType();
    library.Books = new BooksType();
    library.Books.Book = new List<BookType>();
    BookType newBook = new BookType();
    newBook.Title = "Book 1";
    newBook.Author = "Author 1";
    library.Books.Book.Add(newBook);

    But that doesn’t seem neat enough. I want to write something like: 

    LibraryType library = new LibraryType();
    library.Books = new List<BookType>();
    BookType newBook = new BookType();
    newBook.Title = "Book 1";
    newBook.Author = "Author 1";
    library.Books.Add(newBook);

    Thus, I’ll need to get rid of the class BooksType and change it in the declaration from “private BooksType booksField;” to “private List<BookType> booksField;”. However, making that change only is not enough. We need to tell the serializer that the new property is an XmlArrayItem and not an XmlElement. Take a look at the resulting code in Library.cs.  

    Tip 3 – Serializing Object to Xml 

    Now, we should be able to write code and generate Xml from the object created. For example, writing the code below 

    //  Create a library
    LibraryType library = new LibraryType();
    
    //  Create Books tag
    library.Books = new List<BookType>();
    
    //  Add 5 books to the library
    for (int i = 1; i <= 5; i++)
    {
        BookType book = new BookType();
        book.Title = string.Format("Book {0}", i);
        book.Author = string.Format("Author {0}", i);
        library.Books.Add(book);
    }
    
    //  Create employees tag
    library.Employees = new List<EmployeeType>();
    
    //  Add 3 employees to the library
    for (int i = 1; i <= 3; i++)
    {
        EmployeeType employee = new EmployeeType();
        employee.Name = string.Format("Book {0}", i);
        library.Employees.Add(employee);
    }
    
    //  Now that the object is created, serialize it and print out resulting Xml
    XmlSerializer serializer = new XmlSerializer(typeof(LibraryType));
    StringWriter sw = new StringWriter();
    serializer.Serialize(sw, library);
    Console.WriteLine("Object serialized to Xml:\n\n{0}", sw.ToString());

    would result in the following Xml 

    <?xml version="1.0" encoding="utf-16"?>
    <Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.ali.com/lib/">
      <Books>
        <BookType Title="Book 1" Author="Author 1" />
        <BookType Title="Book 2" Author="Author 2" />
        <BookType Title="Book 3" Author="Author 3" />
        <BookType Title="Book 4" Author="Author 4" />
        <BookType Title="Book 5" Author="Author 5" />
      </Books>
      <Employees>
        <EmployeeType Name="Book 1" />
        <EmployeeType Name="Book 2" />
        <EmployeeType Name="Book 3" />
      </Employees>
    </Library>

    Tip 4 – Serializing without Namespace 

    If you look at the serialized Xml above, you’ll notice the extra xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“,  xmlns:xsd=”http://www.w3.org/2001/XMLSchema”  and xmlns=”http://schemas.ali.com/lib/“. These namespaces are added by default. In my case, I only care about the  xmlns=”http://schemas.ali.com/lib/” which is the url for the XSD of my Xml file. To get rid of the above namespaces and keep the one referring to the XSD, we’ll need to pass our custom XmlSerializerNamespaces object to the Serialize() method. 

    //  Create our own xml serializer namespace
    //  Avoiding default xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    //  and xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
    
    //  Add lib namespace with empty prefix
    ns.Add("", "http://schemas.ali.com/lib/"); 
    
    //  Now serialize by passing the XmlSerializerNamespaces object
    //  as a parameter to the Serialize() method
    XmlSerializer serializer = new XmlSerializer(typeof(LibraryType));
    StringWriter sw = new StringWriter();
    serializer.Serialize(sw, library, ns);
    Console.WriteLine("Object serialized to Xml:\n\n{0}", sw.ToString());

    If you want to get rid of the namespaces altogether, you can simply write ns.Add(“”, “”) instead of ns.Add(“”, “http://schemas.ali.com/lib/“)

    Tip 5 – Changing Encoding

    If you look at the generated Xml above, you’ll notice in the Xml declaration that the encoding is set to utf-16. To make this UTF8 encoding, we’ll need to change the Stream object settings before we do the serialization. To do this, replace the code below

    StringWriter sw = new StringWriter();
    serializer.Serialize(sw, library, ns);

    with

    //  Serialize the object to Xml with UTF8 encoding
    MemoryStream ms = new MemoryStream();
    XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, Encoding.UTF8);
    xmlTextWriter.Formatting = Formatting.Indented;
    serializer.Serialize(xmlTextWriter, library, ns);
    ms = (MemoryStream)xmlTextWriter.BaseStream;
    string xml = Encoding.UTF8.GetString(ms.ToArray());

    To make this more generic, we can create a static method that can do serialization with any encoding.

    /// <summary>
    /// Serializes the object to Xml based on encoding and name spaces.
    /// </summary>
    /// <param name="serializer"></param>
    /// <param name="encoding"></param>
    /// <param name="ns"></param>
    /// <param name="objectToSerialize"></param>
    /// <returns></returns>
    public static string Serialize(XmlSerializer serializer,
                               Encoding encoding,
                               XmlSerializerNamespaces ns,
                               object objectToSerialize)
    {
        MemoryStream ms = new MemoryStream();
        XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, encoding);
        xmlTextWriter.Formatting = Formatting.Indented;
        serializer.Serialize(xmlTextWriter, objectToSerialize, ns);
        ms = (MemoryStream)xmlTextWriter.BaseStream;
        return encoding.GetString(ms.ToArray());
    }

    Now we can write something like the below

    string xml = Serialize(serializer, Encoding.UTF8, ns, library);

    Tip 6 – Removing Xml Declaration

    Let’s say you want to completely remove the Xml Declarartion <?xml Version=”1.0″ Encoding=”utf-8″?> from your serialized Xml. You can do so neatly by using an XmlWriterSettings class and setting its OmitXmlDeclaration property to true. Here is how the above Serialize method would change to support this:

    Thus, we can do something like the below to omit the Xml declaration

    /// <summary>
    /// Serializes the object to Xml based on encoding and name spaces.
    /// </summary>
    /// <param name="serializer">XmlSerializer object (passing as param to avoid creating one every time)</param>
    /// <param name="encoding">The encoding of the serialized Xml</param>
    /// <param name="ns">The namespaces to be used by the serializer</param>
    /// <param name="omitDeclaration">Whether to omit Xml declarartion or not</param>
    /// <param name="objectToSerialize">The object we want to serialize to Xml</param>
    /// <returns></returns>
    public static string Serialize(XmlSerializer serializer,
                                   Encoding encoding,
                                   XmlSerializerNamespaces ns,
                                   bool omitDeclaration,
                                   object objectToSerialize)
    {
        MemoryStream ms = new MemoryStream();
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = omitDeclaration;
        settings.Encoding = encoding;
        XmlWriter writer = XmlWriter.Create(ms, settings);
        serializer.Serialize(writer, objectToSerialize, ns);
        return encoding.GetString(ms.ToArray()); ;
    }

    Tip 7 – Deserializing Xml to Object 

    string xml = Serialize(serializer, Encoding.Default, ns, true, library);

    This functionality is really cool. Load Xml into an object I can understand and easily interact with. For example, you’d write the following code to read the contents of Xml file “Sample1.xml” and convert it to object LibraryType.

    //  Read the first Xml file
    TextReader tr = new StreamReader("Sample1.xml");
    
    //  Deserialize the Xml file into a LibraryType object
    XmlSerializer serializer = new XmlSerializer(typeof(LibraryType));
    LibraryType lib1 = (LibraryType)serializer.Deserialize(tr);

    If you look at the object lib1 using the debugger, you’ll see that it’s properly loaded:

    LibraryType Object
    LibraryType Object

    We can add a new book to this object and serialize it back to Xml using the following code

    if (lib1.Books == null)
    {
        lib1.Books = new List<BookType>();
    }
    
    BookType newBook = new BookType();
    newBook.Title = "Book 3";
    lib1.Books.Add(newBook);
    
    //  Serialize back the library type object and output Xml
    StringWriter sw = new StringWriter();
    serializer.Serialize(sw, lib1);
    Console.WriteLine("{0}:\n\n{1}", "Sample1.xml", sw.ToString());

    The resulting Xml would look something like:

    <?xml version="1.0" encoding="utf-16"?>
    <Library xmlns="http://schemas.ali.com/lib/">
      <Books>
        <Book Title="Book 1" Author="Ali" />
        <Book Title="Book 2" Author="Sara" />
        <Book Title="Book 3" />
      </Books>
      <Employees>
        <Employee Name="Ali" />
        <Employee Name="Sara" />
      </Employees>
    </Library>

    Tip 8 – Resolving Empty Lists Issue

    Now, let’s try the same deserialization code above, but on xml file “Sample2.xml” which has no <Employees> tag. When we deserialize xml into an object then serialize back into Xml, we get the following:

    <?xml version="1.0" encoding="utf-16"?>
    <Library xmlns="http://schemas.ali.com/lib/">
      <Books>
        <Book Title="Book 1" Author="Ali" />
        <Book Title="Book 2" Author="Sara" />
      </Books>
      <Employees />
    </Library>

    Notice the extra <Employees/> which we really didn’t intend to have in our Xml. I guess the reason for this issue is because XmlSerializer is initializing all List<T> variables in the object on deserialization (verified that by watching the object in the debugger after deserialization) and thus we’ll get this empty tag <Employees/> when we serialize back. To resolve this issue, there are two workarounds. The first approach is definitely better, but you might find the other approach helpful based on your needs.

    Approach 1

    Don’t bother with the empty lists. Just clean up their corresponding empty Xml tags on serialization. The method below takes a string represenation of the Xml and removes all empty tags.

    /// <summary>
    /// //////////Deletes empty Xml tags from the passed xml
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    public static string CleanEmptyTags(String xml)
    {
        Regex regex = new Regex(@"(\s)*<(\w)*(\s)*/>");
        return regex.Replace(xml, string.Empty);
    }

    With the method above in mind, our Serialize method would change as follows:

    public static string Serialize(XmlSerializer serializer,
                                   Encoding encoding,
                                   XmlSerializerNamespaces ns,
                                   bool omitDeclaration,
                                   object objectToSerialize)
    {
        MemoryStream ms = new MemoryStream();
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = omitDeclaration;
        settings.Encoding = encoding;
        XmlWriter writer = XmlWriter.Create(ms, settings);
        serializer.Serialize(writer, objectToSerialize, ns);
        string xml = encoding.GetString(ms.ToArray());
        xml = CleanEmptyTags(xml);
        return xml;
    }

    Approach 2

    Call the deserialize as usual then set any empty instantiated lists (Count == 0) to null. Here is the static method and its helper method that does the job.

    
    /// <summary>
    /// Deserializes the passed Xml then deallocates any instantiated and empty lists.
    /// </summary>
    /// <param name="serializer"></param>
    /// <param name="tr"></param>
    /// <param name="objectNamespace"></param>
    /// <returns></returns>
    public static object Deserialize(XmlSerializer serializer, TextReader tr, string objectNamespace)
    {
        //  Deserialize Xml into object
        object objectToReturn = serializer.Deserialize(tr);
    
        //  Clean up empty lists
        CleanUpEmptyLists(objectToReturn, objectNamespace);
    
        return objectToReturn;
    }
    
    /// <summary>
    /// Sets any empty lists in the passed object to null. If the passed object itself is a list,
    /// the method returns true of it's empty and false otherwise.
    /// </summary>
    /// <param name="o"></param>
    /// <param name="objectNamespace"></param>
    /// <returns></returns>
    public static bool CleanUpEmptyLists(object o, string objectNamespace)
    {
        //  Skip if the object is already null
        if (o == null)
        {
            return false;
        }
    
        //  Get the types of the object
        Type type = o.GetType();
    
        //  If this is an empty list, set it to null
        if (o is IList)
        {
            IList list = (IList)o;
    
            if (list.Count == 0)
            {
                return true;
            }
            else
            {
                foreach (object obj in list)
                {
                    CleanUpEmptyLists(obj, objectNamespace);
                }
            }
    
            return false;
        }
        //  Ignore any objects that aren't in our namespace for perf reasons
        //  and to avoid getting errors on trying to get into every little detail
        else if (type.Namespace != objectNamespace)
        {
            return false;
        }
    
        //  Loop over all properties and handle them
        foreach (PropertyInfo property in type.GetProperties())
        {
            //  Get the property value and clean up any empty lists it contains
            object propertyValue = property.GetValue(o, null);
            if (CleanUpEmptyLists(propertyValue, objectNamespace))
            {
                property.SetValue(o, null, null);
            }
        }
    
        return false;
    }

    Using the above static method, you can now deserialize as follows:

    //  Deserialize the Xml file into a LibraryType object
    XmlSerializer serializer = new XmlSerializer(typeof(LibraryType));
    LibraryType lib = (LibraryType)Deserialize(serializer, tr, typeof(LibraryType).Namespace);

    If you know of a better solution, please let me know.

    Source

    Download full source code here. You can use it to try out the above tips one by one.

     
    • Roberto 1:36 pm on June 30, 2010 Permalink | Reply

      Hi, good article.

      If I need close braket like this:

      not like this:

      Is posible?

      Thanks

    • Roberto 1:53 pm on June 30, 2010 Permalink | Reply

      Sorry, dont see.
      the idea is dont have end braket like this “/>”

    • ChristianProgrammer 1:13 pm on July 7, 2010 Permalink | Reply

      Superb !!

      Thanks for your contribution

    • urdu sms 8:27 pm on December 3, 2011 Permalink | Reply

      Youre completely correct with this writing.

    • Mohammad Shahjahan Ahmed Talukder 5:27 am on September 19, 2012 Permalink | Reply

      great article, really really nice :)

    • hack rage of bahamut 5:14 am on August 5, 2014 Permalink | Reply

      Hurrah! After all I goot a webpage from where I can truly take vaaluable
      information concerning my study and knowledge.

    • clash of clans gemmes hack 8:55 pm on August 14, 2014 Permalink | Reply

      Good way of explaining, and good article to obtain data concerning my presentation subject matter, which i am going to present in school.

    • rueda.wallenborn.net 11:58 pm on August 14, 2014 Permalink | Reply

      Thanks for ones marvelous posting! I seriously enjoyed reading
      it, you will be a great author.I will ensure that
      I bookmark your blog and definitely will come back very soon. I want to encourage you to definitely continue your great
      writing, have a nice morning!

    • blog.mojewpisy.pl 3:42 pm on September 8, 2014 Permalink | Reply

      Awesome blog! Do you have any suggestions for aspiring writers?
      I’m planning to start my own website soon but I’m
      a little lost on everything. Would you advise starting
      with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely confused ..
      Any recommendations? Thanks!

    • http://www.salope-asiatique.info 12:24 am on September 23, 2014 Permalink | Reply

      Puis-je vous piquuer 2 ou 3 paragraphes sսr un site internet perso ?

    • Yelper 1:39 am on September 25, 2014 Permalink | Reply

      Besides only because a little character has invested in a single lens photo camera does
      not signify that she or he is a kind photographer. Be it a new camera equipment
      or fashion adornments, to aid photographers to detach themselves from the regular ones.
      With Sara, your wedding day will be captured in the style of a
      documentary, covering not just the major events
      of the day but the minor ones also – such as preparation and shots of what your
      friends & family were getting up to at your reception when you weren.

    • videos porno gratis 7:32 am on September 25, 2014 Permalink | Reply

      Un postе vraiment plein de vérités

    • telecharger du sexe 1:29 pm on September 25, 2014 Permalink | Reply

      Јe pensais justement faire un article semblable au votre

    • monitoring and security 7:59 pm on October 1, 2014 Permalink | Reply

      Greetings from Florida! I’m bored to tears at work so I decided to browse your blog on my
      iphone during lunch break. I love the info you present here and can’t wait to take a look
      when I get home. I’m shocked at how quick your
      blog loaded on my cell phone .. I’m not even using WIFI, just 3G ..

      Anyways, great site!

    • Envoi de MMS marketing 5:45 pm on October 9, 2014 Permalink | Reply

      Tentez nos programmes pour vos envois de MMS. Réalisé pour les particuliers, ce
      programme vous permet depuis internet de rédiger vos envois à
      destination de tous vos clients. Réalisée por expédier de très nombreyx envois, notre
      plateforme MMS est d’une fiabilité sazns égale
      pour effectuer vos campagnes en masse.
      Doté d’une puissante fonctionnalité de back office, notre service est le summu des
      systèmes d’envoi de MMS et de MMS.
      Vous réalisez rapidement vos messages à destination de milliers de contacts.

    • salopes blanches 10:02 am on October 15, 2014 Permalink | Reply

      Unee fߋis de plus unn article vraimenjt instruϲtif

    • sexe avec violence 5:14 am on December 23, 2014 Permalink | Reply

      Ϲe post esst rempli de vérité

  • Ali BaderEddin 5:49 am on May 14, 2008 Permalink | Reply
    Tags: CAML, , , Visual Studio, wss, , XSD   

    SharePoint CAML IntelliSense 


    So you’ve been having some pain trying to put IntelliSense into your frequent CAML (Collaborative Application Markup Language) files. For every new XML file you create, you have to go to its properties and then point to the target schema located at ../TEMPLATE/XML/wss.xsd.

    If you don’t know what I mean, usually, in order to display IntelliSense in an XML file opened through Visual Studio, we must link the XML file to an XSD. This is done by going to the file properties and linking to the XSD every time we open the XML file in Visual Studio.

    One way to avoid having to link to the XSD every time, is to add the file(s) to a Visual Studio project. However, what if you create CAML files so frequently that you don’t want to add them to a Visual Studio project every time?

    The best solution would be to load the XSD whenever a new XML file referencing it is opened; and I have the steps to do that!

    Under the Schemas folder of the Visual Studio installation folder (Ex: C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas), there is a file named Catalog.xml.

    Reference wss.xsd from it by adding the following tag:

    Make sure that href is pointing to the correct location of the wss.xsd file.

    <Schema href="C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/12/TEMPLATE/XML/wss.xsd" targetNamespace="http://schemas.microsoft.com/sharepoint/" />

    Enjoy!

    Ali B

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: