我有一个 Web 应用程序,可以根据通过查询字符串传入的条件动态创建 XML 提要。
这是一个非常简单的应用程序:它读取查询字符串变量,并使用它们将数据(来自数据库的缓存数据)格式化为适当的 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。哇,效率太低了。将更新代码。谢谢大家!