簡體   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