Silverlight uses IsolatedStorage to create a new XML file and query XML with LINQ

Source: Internet
Author: User
Tags silverlight

code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/-->using System;usingSystem.Linq;usingSystem.Windows.Shapes;usingSystem.Collections.Generic;usingSystem.Collections.ObjectModel;usingSystem.IO.IsolatedStorage;usingSystem.IO;usingSystem.Xml.Linq;usingSystem.Xml;namespaceservicefacade{ Public classFresourcecategorycode {ReadOnly stringstrFileName ="Resourcecategorycode.xml"; List<FResourceCategoryCodeModel> Lfrccs =NewList<fresourcecategorycodemodel>();/// <summary>///generate an XML cache file/// </summary> Public voidCreateFile (list<fresourcecategorycodemodel>lfrccm) {Try{using(IsolatedStorageFile Isostore =isolatedstoragefile.getuserstoreforapplication ()) {using(IsolatedStorageFileStream Isostream =NewIsolatedStorageFileStream (Strfilename,filemode.create, Isostore)) {//Store one message per userXmlWriterSettings settings =Newxmlwritersettings (); settings. Indent=true;//create a XmlWriter.using(XmlWriter writer =xmlwriter.create (Isostream, Settings)) {//set the root of the XMLWriter. WriteStartElement ("Resourcecategorys");foreach(varVinchlfrccm) {//set the root of each elementWriter. WriteStartElement ("resourcecategory");//here are the settings for each element and valueWriter. WriteStartElement ("Cnccategroystandard"); writer. WriteString (V.cnccategroystandard); writer. WriteEndElement (); writer. WriteStartElement ("Cnvccategorycode"); writer. WriteString (V.cnvccategorycode); writer. WriteEndElement (); writer. WriteStartElement ("Cnvccategoryname"); writer. WriteString (V.cnvccategoryname); writer. WriteEndElement (); writer. WriteStartElement ("Cnilevel"); writer. WriteString (Convert.ToString (V.cnilevel)); writer. WriteEndElement (); writer. WriteStartElement ("Cnvcparentcode"); writer. WriteString (V.cnvcparentcode); writer. WriteEndElement (); writer. WriteStartElement ("Cniorderid"); writer. WriteString (Convert.ToString (V.cniorderid)); writer. WriteEndElement ();//XML end Tagwriter. WriteEndElement ();}//XML root end tagwriter. WriteEndElement (); writer. Flush (); }}}}Catch{;}}/// <summary>        ///reading cache files/// </summary>        Private voidReadXml () {using(IsolatedStorageFile store =isolatedstoragefile.getuserstoreforapplication ()) {                //read the file and convert the file to StreamReaderStreamReader reader =NewStreamReader (store.                OpenFile (strFileName, FileMode.Open, FileAccess.Read)); //Convert a string to XMLXDocument xmlstory =Xdocument.parse (reader.                ReadToEnd ()); //converting to a collection with LINQ to XML               varVardetails = fromDetailsinchXmlstory.descendants ("resourcecategory")//only information that is node resourcecategory is obtained.                                 Select NewFresourcecategorycodemodel {Cnccategroystandard = (string) details. Element ("Cnccategroystandard"), Cnvccategorycode= (string) details. Element ("Cnvccategorycode"), Cnvccategoryname= (string) details. Element ("Cnvccategoryname"), Cnilevel= (int) details. Element ("Cnilevel"), Cniorderid= (int) details. Element ("Cniorderid"), Cnvcparentcode= (string) details. Element ("Cnvcparentcode")                                 }; Lfrccs= vardetails.tolist<fresourcecategorycodemodel>(); Store.            Dispose (); }        }/// <summary>        ///entity class/// </summary>   Public classFresourcecategorycodemodel {Private int_cnicategroyid; /// <summary>        ///Serial Number/// </summary>         Public intCnicategroyid {Get{return_cnicategroyid;} Set{_cnicategroyid =value;} }        Private string_cnccategroystandard; /// <summary>        ///Standard Classification code/// </summary>         Public stringCnccategroystandard {Get{return_cnccategroystandard;} Set{_cnccategroystandard =value;} }        Private string_cnvccategorycode; /// <summary>        ///Resource classification and coding/// </summary>         Public stringCnvccategorycode {Get{return_cnvccategorycode;} Set{_cnvccategorycode =value;} }        Private string_cnvccategoryname; /// <summary>        ///Resource Category name/// </summary>         Public stringCnvccategoryname {Get{return_cnvccategoryname;} Set{_cnvccategoryname =value;} }        Private int_cnilevel; /// <summary>        ///Resource Classification Hierarchy/// </summary>         Public intCnilevel {Get{return_cnilevel;} Set{_cnilevel =value;} }        Private string_cnvcparentcode; /// <summary>        ///Resource Classification Parent Code/// </summary>         Public stringCnvcparentcode {Get{return_cnvcparentcode;} Set{_cnvcparentcode =value;} }        Private int_cniorderid; /// <summary>        ///Sort ID/// </summary>         Public intCniorderid {Get{return_cniorderid;} Set{_cniorderid =value;} }        Private string_cnvcremark; /// <summary>        ///Description Text/// </summary>         Public stringCnvcremark {Get{return_cnvcremark;} Set{_cnvcremark =value;} }}}}

Then by calling the method CreateFile and ReadXml can be implemented, the file storage address is the independent space of SL, the storage address under the XP system is C:\Documents and Settings\ user name \local Settings\ Application Data\microsoft\silverlight directory, you can find many cache files

Article turned from: http://www.cnblogs.com/888h/archive/2010/10/20/1856407.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.