.NET XmlReader 与“FOR XML” 数据
最近,在公司中,我们从一些旧项目中获得了一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要根据返回的 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.