Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML serialization, encoding

using System;

public class clsPerson
{
  public  string FirstName;
  public  string MI;
  public  string LastName;
}

class class1
{ 
   static void Main(string[] args)
   {
      clsPerson p=new clsPerson();
      p.FirstName = "Jeff";
      p.MI = "A";
      p.LastName = "Price";
      System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
      x.Serialize(Console.Out, p);
      Console.WriteLine();
      Console.ReadLine();
   }
} 

taken from http://support.microsoft.com/kb/815813

1)

System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());

What does this line do? what is GetType()?

2) how do I get the encoding to

<?xml version="1.0" encoding="utf-8"?>
< clsPerson xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

instead of

<?xml version="1.0" encoding="IBM437"?>
 <clsPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3
 .org/2001/XMLSchema">

or not include the encoding type at all?

like image 207
001 Avatar asked Feb 08 '11 00:02

001


People also ask

What is XML serialization?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

Is XML serializable?

XML serialization can take more than one form, from simple to complex. For example, you can serialize a class that simply consists of public fields and properties, as shown in Introducing XML Serialization.

Why do we serialize XML?

Advantages of Using XML SerializationThe XmlSerializer class gives you complete and flexible control when you serialize an object as XML. If you are creating an XML Web service, you can apply attributes that control serialization to classes and members to ensure that the XML output conforms to a specific schema.

What is the basic difference between SOAP serialization and XML serialization?

- SoapFormatter is better to serialize arbitrary data. - XML Serialization would just serialize the public properties of an object. - The XML Serializer is used typically for serializing simple types across web services. - Xmlserializer is better, more flexible and faster.


4 Answers

If you pass the serializer an XmlWriter, you can control some parameters like encoding, whether to omit the declaration (eg for a fragment), etc.

This is not meant to be a definitive guide, but an alternative so you can see what's going on, and something that isn't just going to console first.

Note also, if you create your XmlWriter with a StringBuilder instead of a MemoryStream, your xml will ignore your Encoding and come out as utf-16 encoded. See the blog post writing xml with utf8 encoding for more information.

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings 
{ 
    Indent = true, 
    OmitXmlDeclaration = false, 
    Encoding = Encoding.UTF8 
};

using (MemoryStream memoryStream = new MemoryStream() )
using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings))
{   
    var x = new System.Xml.Serialization.XmlSerializer(p.GetType());
    x.Serialize(xmlWriter, p);

    // we just output back to the console for this demo.
    memoryStream.Position = 0; // rewind the stream before reading back.
    using( StreamReader sr = new StreamReader(memoryStream))
    {
        Console.WriteLine(sr.ReadToEnd());
    } // note memory stream disposed by StreamReaders Dispose()
}
like image 80
Robert Paulson Avatar answered Nov 07 '22 09:11

Robert Paulson


1) The GetType() function returns a Type object representing the type of your object, in this case the class clsPerson. You could also use typeof(clsPerson) and get the same result. That line creates an XmlSerializer object for your particular class.

2) If you want to change the encoding, I believe there is an override of the Serialize() function that lets you specify that. See MSDN for details. You may have to create an XmlWriter object to use it though, details for that are also on MSDN:

 XmlWriter writer = XmlWriter.Create(Console.Out, settings);

You can also set the encoding in the XmlWriter, the XmlWriterSettings object has an Encoding property.

like image 30
WildCrustacean Avatar answered Nov 07 '22 09:11

WildCrustacean


I took the solution offered by @robert-paulson here for a similar thing I was trying to do and get the string of an XmlSchema. By default it would return as utf-16. However as mentioned the solution here suffers from a Stream Closed Read error. So I tool the liberty of posting the refactor as an extension method with the tweek mentioned by @Liam to move the using block.

    public static string ToXmlString(this XmlSchema xsd)
    {
        var xmlWriterSettings = new XmlWriterSettings
        {
            Indent = true,
            OmitXmlDeclaration = false,
            Encoding = Encoding.UTF8
        };

        using (var memoryStream = new MemoryStream())
        {
            using (var xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings))
            {
                xsd.Write(xmlWriter);
            }

            memoryStream.Position = 0; 
            using (var sr = new StreamReader(memoryStream))
            {
                return sr.ReadToEnd();
            }
        }
    }
like image 28
DubMan Avatar answered Nov 07 '22 11:11

DubMan


1) This creates a XmlSerializer for the class clsPerson.

2) encoding is IBM437 because that is the form for the Console.Out stream.

PS: Hungarian notation is not preferred in C#; just name you class Person.

like image 41
Richard Schneider Avatar answered Nov 07 '22 10:11

Richard Schneider