简体   繁体   中英

C# Best way to hold xml data

Im requesting data from web service and receive a xml file.Not do flood the service good idea would be to cache/store the xml so when the application is started again it would use that cached xml. The data in received xml will change in every 24 hours so afther that time is passed from old request application must create new one anyway.

What would be the best solution to keep that data?

EDIT: Maybe use SQLite to keep some history?

You can just stream it to a file:

/// 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));

If I'm understanding you correctly, you might consider loading the data into an XmlDocument or XDocument and storing it in cache with a 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"); 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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