如何解决从XmlReader.ReadInnerXml保存XmlDocument时出现OutOfMemoryException
我正在使用XmlReader.ReadInnerXml()加载XML文件的一部分并将其另存为XmlDocument。当innerXml部分超过2 GB(估计)时,我遇到了OutOfMemoryException。处理此错误的最佳方法是什么?有没有更好的方法可以从XmlReader创建大型xml?我可以保存内容而不加载到内存中吗?
using (XmlReader xmlRdr = XmlReader.Create(file))
{
xmlRdr.MoveToContent();
while (xmlRdr.Read())
{
//when read to XmlNodeType.Element and xmlRdr.Name meets certain criteria
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
try
{
xmlDoc.LoadXml(xmlRdr.ReadInnerXml());
//get a few data from within the innerXml and eventually use XmlWritter to save the file
}
catch(Exception e)
{
string content = $"{e.GetType()} {e.Message} {NewLine} {objId}";
//send content to log file and email
}
}
}
解决方法
如评论中所述,可以尝试使用StreamReader和StreamWriter
此tutorial可能有帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。