.NET XmlReader 与“FOR XML” 数据

发布于 2024-07-25 17:54:38 字数 1094 浏览 1 评论 0原文

最近,在公司中,我们从一些旧项目中获得了一些 MSSQL 数据库,我们必须将其集成到当前的解决方案中。

数据库有大约 100-150 个使用 FOR XML AUTO 子句的存储过程,以便查询以 XML 而不是行的形式返回完整的对象图。

最快的解决方案(对于我们公司来说)是根据从数据库返回的 xml 数据创建可序列化的类(使用 xsd-tool)。

这是我们用来实例化这些对象的代码:

 public static T GetObjectFromXml<T>(DbCommand command)
    {
        SqlDatabase db = (SqlDatabase)DB;
        XmlReader xmlReader = null;
        T returnValue;

        xmlReader = db.ExecuteXmlReader(command);
        xmlReader.MoveToContent();

        XmlSerializer serializer = new XmlSerializer(typeof(T));

        returnValue = (T)serializer.Deserialize(xmlReader);

        xmlReader.Close();

        return returnValue;


    }

DB 代表企业库中的数据库类。

当 sp 返回大量数据(例如,其中包含大量子对象、孙子对象、grandgrndchldrn...对象的某个大对象集合)时,此方法的执行会持续很长时间。

应用程序中的数据肯定会继续增长,我必须考虑对此进行优化。

所以,我想知道这是否是不好的做法(使用 FORXML、XmlReader 和 Deserialize),或者我们应该重写存储过程并使用 SqlDataReaders 或 Linq2Sql,或者此代码片段中存在一些 perf.issue(不正确使用泛型或其他内容) )?


编辑 我知道一次加载大量数据是不好的做法,并且我知道加载过程应该分成更小的块,但我只是想知道这段特定的代码是否有问题。

Recently, in company we got some MSSQL database from some old project which we have to integrate in current solution.

Database has about 100-150 stored procedures that use FOR XML AUTO clause, so that queries return complete object-graph as XML instead of rows.

Quickest solution (for us in company) was to create serializable classes (with xsd-tool) based on the xml data returned from database.

This is code we use to instatiate those objects:

 public static T GetObjectFromXml<T>(DbCommand command)
    {
        SqlDatabase db = (SqlDatabase)DB;
        XmlReader xmlReader = null;
        T returnValue;

        xmlReader = db.ExecuteXmlReader(command);
        xmlReader.MoveToContent();

        XmlSerializer serializer = new XmlSerializer(typeof(T));

        returnValue = (T)serializer.Deserialize(xmlReader);

        xmlReader.Close();

        return returnValue;


    }

DB represents Database class from enterprise library.

When sp returns lots of data (for example some big collection of objects with lots of children,grandchildren,grandgrndchldrn...objects in it) execution of this method lasts very long.

The data in application will surrely continue to grow, and I must think of optimizing this.

So, I'm wondering if this is bad practice (using FORXML, XmlReader and Deserialize), or we should rewrite stored procedures and use SqlDataReaders or Linq2Sql, or there is some perf.issue within this snippet (improper use of generics or something else) ?


Edit
I know that it is bad practice to load big ammount of data at once, and I know that load process should be splitted to smaller chunks, but I'm just wondering if something is wrong with this particular piece of code.

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

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

发布评论

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

评论(1

笨笨の傻瓜 2024-08-01 17:54:38

您需要根据返回的 XML 中的内容来分析此问题。 XML 返回的数据是否不需要一次性全部存储在内存中? 然后将其全部反序列化到内存中可能不是最好的事情。 如果您只需要一次处理一点数据,那么您也许应该将 XML 作为 XML 处理,并保留 XmlReader 以一次读取一点数据。

You need to analyze this problem in terms of what's in the XML being returned. Is the XML returning data that doesn't need to be in memory all at once? Then deserializing it all into memory is probably not the best thing. If you only need to process the data a little at a time, then you should perhaps process the XML as XML and keep the XmlReader around to read a little at a time.

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