我有一个web应用程序,它根据通过查询字符串传入的条件来动态创建XML提要。CPU密集型XmlTextWriter
这是一个非常简单的应用程序:它读取查询字符串变量,并使用它们将数据(从数据库缓存的数据)格式化为适当的XML格式。
返回的XML文件大约是25MB ......有很多数据。
我正在使用XmlTextWriter构建XML,然后将其作为“应用程序/八位字节流”(它可以下载的附件)返回给请求者。
问题是:构建XML似乎使用了100%的CPU,并导致我的其他应用程序出现问题。
有没有人有建设如此大的XML文件的经验?任何关于使这个过程更少CPU密集的提示?
代码示例:
map.WriteRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
map.WriteStartElement("rss");
map.WriteAttributeString("version", "2.0");
map.WriteAttributeString("xmlns:g", "http://base.google.com/ns/1.0");
map.WriteStartElement("channel");
map.WriteElementString("link", "http://www.mywebsite.com...");
ProductCollection items = Product.GetCachedSiteProducts();
foreach (Product p in items)
{
map.WriteStartElement("item");
........
map.WriteElementString("description", p.Description);
map.WriteElementString("g:id", p.SiteSku);
map.WriteElementString("g:condition", "new");
map.WriteElementString("g:price", p.Price.ToString() + " USD");
...............
map.WriteEndElement(); //item
}
}
map.WriteEndElement();//channel
map.WriteEndElement();//rss
Response.Write(sw.ToString());
更新:我回答我的问题..感谢那些谁问我要发布更多的代码,这是一个容易当我看着更仔细。
代码使用“Response.write(map.ToString())”输出xml。哇,这是低效的。将更新代码。谢谢大家!
你可以发布你实际用于构建XMLStream的代码,也许这里有一些消费问题..没有看到我认为的代码 – MethodMan 2012-01-09 17:30:10
有没有办法可以创建一个需要的shell值要输入,即不要重建100%的时间?你有没有考虑缓存回应? – 2012-01-09 17:30:43
我们构建的XML文件> 100 MB,并且不使用100%的CPU ...所以我怀疑以某种方式帮助您需要显示一些源代码... – Yahia 2012-01-09 17:34:42