.XmlSerializers.dll 如何使用它 - 互联网上的示例令人困惑

发布于 2024-11-06 07:07:49 字数 2950 浏览 0 评论 0原文

我是 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 序列化总是尝试 Assembly.Load() 上 无论如何,.XmlSerializers.dll 程序集。 <强>>另外,你永远不会引用 生成的 XmlSerializationWriterXxx 和 XmlSerializationReaderXxx 类 直接在您的代码中。

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

编辑:或者也许我误解了引用文章中的某些内容,而两个人都正确?我迷路了:)

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭白衣梦中忆 2024-11-13 07:07:49

“添加引用是不必要的”恰恰意味着 - 没有必要。

即使您不打算引用程序集(和其他文件),也有理由将它们添加到您的项目中。即,如果您想要创建安装项目或发布解决方案,您希望从解决方案中引用需要部署的所有文件,即使某些文件不直接由代码使用。

我个人不建议过多深入研究这个主题,因为初始学习经验 - 通常运行时生成的程序集适用于大多数场景。

"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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文