3

I'm quite newbie to C#. I've started to use SGEN generated XmlSerializers.dll and I'm really confused right now. Despite that I cannot find any true step by step tutorial how to use it properly I'm also confused by different advices.

I read a lot of SGEN articles and I'm still not sure how to use generated lib in my project.

Does any one who has real-coding practice with this can explain me once and for all the proper way to use it?

I was thinking that I understood how to use it but yesterday I found this tutorial: http://www.dotnetfunda.com/articles/article1105-optimized-way-of-xml-serialization-using-sgen-utility-.aspx

The guy has added .XmlSerializers.dll to his Project references and uses code like this to Serialize:

static string SerializebySGEN()
{
  Person p = new Person();
  p.Age = 29;
  p.Name = "Satya Narayan Sahoo";
  StringBuilder buildr = new StringBuilder();
  StringWriter writr = new System.IO.StringWriter(buildr);
  PersonSerializer mySerialzer = new PersonSerializer();
  mySerialzer.Serialize(writr, p);
  string str = writr.ToString();
  return str;
}

PersonSerializer mySerialzer = new PersonSerializer();

but on stackoverflow in the past somebody wrote to my another question connected to XmlSerializers:

Adding a reference is not necessary, Xml serialization always tries an Assembly.Load() on the .XmlSerializers.dll assembly anyway. > Plus, you'll never reference the generated XmlSerializationWriterXxx and XmlSerializationReaderXxx classes directly in your code.

So who is right? Can somne practitioner tell me how I should use this SGEN generated library with my project and code? I really want to use it in good way! :)

Edit: or maybe I misudnerstood something in quoted articles and both person have right? I'm lost :)

Edit2: I wrote below method to deserialize one of my Serializable Classes (MySerializableClass) and I use SGEN generated class MySerializableClassSerializer. Is this ok? (I think so, plz confirm ;))

        /// <summary>
        /// Deserializes the specified XML source into object using SGEN generated class.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xmlSource">The XML source.</param>
        /// <param name="isFile">if set to <c>true</c> the the source is a text File else it is a XML String.</param>
        /// <returns>Return object with deserialized XML</returns>
        public static MySerializableClass MySerializableClassSgenDeserialize(string xmlSource, bool isFile = true)
        {
            MySerializableClass data = new MySerializableClass();

            if (isFile)
            {
                using (TextReader textReader = new StreamReader(xmlSource))
                {
                    MySerializableClassSerializer xSerializer = new MySerializableClassSerializer();
                    data = (MySerializableClass)xSerializer.Deserialize(textReader);
                }
            }
            else
            {
                using (StringReader xmlText = new StringReader(xmlSource))
                {
                    MySerializableClassSerializer xSerializer = new MySerializableClassSerializer();
                    data = (MySerializableClass)xSerializer.Deserialize(xmlText);
                }
            }

            return data;
        }
binball
  • 2,255
  • 4
  • 30
  • 34

1 Answers1

0

"Adding a reference is not necessary" means precisely that - not necessary.

There are reasons to add assemblies (and other files) to your project even if you are not planning to reference them. I.e. if you want to create setup project or publish solution you want to have all files you need to deploy to be refrenced from the solution even if some are not directly used by code.

I personally would not recommend digging into this topic too much as initial learning expirience - generally runtime generated assemblies work for most scenarios.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • thank you for your answer. Are these methods from SGEN lib PersonSerializer mySerialzer = new PersonSerializer(); used in quoted code correctly? or should I use ordinary XmlSerializer xSerializer = new XmlSerializer(typeof(T));? :| I suppose this question is a little bit stupid but I would like to be sure :O :) – binball May 10 '11 at 22:16
  • In my app I use generated serialization classes for my objects like in this example and all looks fine (XmlSerializers.dll is loaded and etc...) – binball May 10 '11 at 22:23
  • Your code looks fine as it is. If you use XmlSerializer xSerializer = new XmlSerializer(typeof(T)); you don't need to add reference explicitly. – Alexei Levenkov May 10 '11 at 23:01
  • but in dotnetfund tutorial link the guy wrote that if he used: XmlSerializer mySerialzer = new XmlSerializer(typeof(Person)); then serialization is 100% slowest than in case of PersonSerializer mySerialzer = new PersonSerializer(); (this methods are generated by SGEN) – binball May 11 '11 at 07:30