繁体   English   中英

C#保存XML数据的最佳方法

[英]C# Best way to hold xml data

我从Web服务请求数据并接收一个xml文件。不要泛滥该服务,最好的方法是缓存/存储xml,因此当应用程序再次启动时,它将使用该缓存的xml。 接收到的xml中的数据将每24小时更改一次,因此从旧的请求应用程序经过的时间仍然必须创建新的数据。

保留该数据的最佳解决方案是什么?

编辑:也许使用SQLite保留一些历史记录?

您可以将其流式传输到文件中:

/// saving it :
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.IO.File.WriteAllText(filename, doc.Value);

/// loading it back in :
System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
xdoc.LoadXml(System.IO.File.ReadAllText(filename));

如果我对您的理解正确,则可以考虑将数据加载到XmlDocumentXDocument并使用CacheDependency将其存储在缓存中:

XmlDocument xDoc = new XmlDocument(); 

if (Cache.Get("MenuData") == null) 
{ 
    xDoc.Load(Server.MapPath("/MenuData.xml")); 
    Cache.Insert("SiteNav", xDoc, new CacheDependency(Server.MapPath("/MenuData.xml"))); 
} 
else 
{ 
    xDoc = (XmlDocument)HttpContext.Current.Cache.Get("MenuData"); 
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM