3

我是 C# 的新手。我已经开始使用 SGEN 生成的 XmlSerializers.dll,我现在真的很困惑。尽管我找不到任何真正的分步教程如何正确使用它,但我也对不同的建议感到困惑。

我阅读了很多 SGEN 文章,但我仍然不确定如何在我的项目中使用生成的库。

是否有任何对此进行过实际编码练习的人可以一劳永逸地向我解释使用它的正确方法?

我以为我了解如何使用它,但昨天我找到了这个教程: http: //www.dotnetfunda.com/articles/article1105-optimized-way-of-xml-serialization-using-sgen-utility-.aspx

该人已将 .XmlSerializers.dll 添加到他的项目引用中,并使用如下代码进行序列化:

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();

但是在过去的stackoverflow上,有人写信给我与XmlSerializers相关的另一个问题:

不需要添加引用,Xml 序列化总是在 .XmlSerializers.dll 程序集上尝试 Assembly.Load() 。 > 另外,您永远不会直接在代码中引用生成的 XmlSerializationWriterXxx 和 XmlSerializationReaderXxx 类。

那么谁是对的呢?somne​​ 从业者可以告诉我应该如何在我的项目和代码中使用这个 SGEN 生成的库吗?我真的很想好好利用它!:)

编辑:或者我在引用的文章中误解了一些东西,两个人都有权利?我迷路了 :)

Edit2:我编写了以下方法来反序列化我的可序列化类(MySerializableClass)之一,并使用 SGEN 生成的类 MySerializableClassSerializer。这个可以吗?(我想是的,请确认;))

        /// <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;
        }
4

1 回答 1

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.

于 2011-05-10T21:57:06.423 回答