Windows Phone development-XML parsing and SQL ce database experience

Source: Internet
Author: User

Outline:
The content in this article is mainly about parsing local XML (similarly, the XML data parsing method on the Web end is the same), and then saving the parsed XML data to the SQL ce database.
The SQL ce database is created in a program and saved in an independent storage area. Therefore, it is invisible to the project.

Refer to blog:
Http://blog.csdn.net/fengyun1989/article/details/7342774

Part 1: XML Parsing
Figure 11:

1. Make sure that the project contains references of system. xml. Ling and system. xml. Add references without reference.
2. The Code is as follows:

// Read the resource file XML streamresourceinfo Sri = application. getresourcestream (New uri ("/myweatherforecast; component/citycode. XML ", urikind. relative); string result; using (streamreader sr = new streamreader (SRI. stream) {result = sr. readtoend ();} // Parse XML xdocument Doc = xdocument. parse (result); foreach (xelement item in Doc. descendants ("root "). nodes () {string province = item. attribute ("data "). value; foreach (xelement itemnode in item. descendants ("city") {string cityname = itemnode. element ("cityname "). value; string cityid = itemnode. element ("cityid "). value; // store the data to the database cityinfotable cityinfo = new cityinfotable (); cityinfo. citycode = cityid; cityinfo. province = province; cityinfo. cityname = cityname; dB. cityinfos. insertonsubmit (cityinfo );}}

Code Description:
Streamresourceinfo is used to parse resource resources. Therefore, before this step, modify the XML file attribute to resource.
Xdocument receives XML documents
Xelement indicates the XML node Element
Doc. descendants ("root"). nodes () indicates all subnodes under the root node.
Item. Attribute ("data"). Value obtains the value of node data.

The above section is the XML parsing step. You only need to know the name of the corresponding node before parsing.

========================================================== =====
Part 1: Use of SQL Ce (creating databases and querying data)
1. First add the system. Data. Ling reference required by the database
2. Create a database table structure
Create a table structure: cityinfotable. CS

namespace MyWeatherForeCast{   [Table]    public class CityInfoTable    {       private int _cityInfoId;       [Column(IsPrimaryKey=true,IsDbGenerated=true,DbType="INT NOT NULL Identity",CanBeNull=false,AutoSync=AutoSync.OnInsert)  ]        public int CityInfoId {            get { return _cityInfoId; }            set { _cityInfoId = value; }       }       private string _province;        [Column ]       public string Province {           get { return _province; }           set { _province = value; }        }        private string _cityName;        [Column ]        public string CityName {            get { return _cityName; }            set {                _cityName = value;                }        }        private string _cityCode;        [Column]        public string CityCode {            get { return _cityCode; }            set { _cityCode = value; }        }    }}

Code Description:
[Table] indicates that cityinfotable is a table and must contain this field. Otherwise, an error occurs when the database is created because the table does not exist.
_ Cityinfoid, _ province, _ cityname, _ citycode indicates four fields in the table.
[Column] indicates a column in a database table, and [column (...)] indicates the attributes of the column.

3. Create a database table
The citydatacontext. CS file is used to create a database:

Public class citydatacontext: datacontext {// defines the database connection string public static string connectionstring = "Data Source = isostore:/cityinfo. SDF "; // pass the database connection string to the datacontext base class public citydatacontext (string connectionstring): Base (connectionstring) {}// define a data table, if multiple data tables exist, you can add them as the member variable public table <cityinfotable> cityinfos {get {return this. gettable <cityinfotable> ();}}}

Code Description:
Citydatacontext inherits from datacontext.
As the database connection string, connectionstring is stored in the cityinfo. SDF file of the independent storage area.
Cityinfos is the name of the database table.
4. Add data

Private void createdb () {using (citydatacontext DB = new citydatacontext (citydatacontext. connectionstring) {If (dB. databaseexists () = false) {// create a database. createdatabase (); // read the resource file XML streamresourceinfo Sri = application. getresourcestream (New uri ("/myweatherforecast; component/citycode. XML ", urikind. relative); string result; using (streamreader sr = new streamreader (SRI. stream) {result = sr. readtoend ();} // Parse XML xdocument Doc = xdocument. parse (result); foreach (xelement item in Doc. descendants ("root "). nodes () {string province = item. attribute ("data "). value; foreach (xelement itemnode in item. descendants ("city") {string cityname = itemnode. element ("cityname "). value; string cityid = itemnode. element ("cityid "). value; // store the data to the database cityinfotable cityinfo = new cityinfotable (); cityinfo. citycode = cityid; cityinfo. province = province; cityinfo. cityname = cityname; dB. cityinfos. insertonsubmit (cityinfo) ;}// the database submits an update database. submitchanges ();}}}

Code Description:
Cityinfotable cityinfo = new cityinfotable ();
Cityinfo. citycode = cityid;
Cityinfo. Province = province;
Cityinfo. cityname = cityname;
DB. cityinfos. insertonsubmit (cityinfo );
Cityinfo is a created data table. After adding data, add it to the cityinfos database table and submit it.
-------------------------------------
Supplementary query statement:
Query the city information of Beijing and display it in a pop-up window.

1. using (citydatacontext DB = new citydatacontext (citydatacontext. connectionstring) {iqueryable <cityinfotable> queries = from C in dB. cityinfos where c. province = "Beijing" & C. cityname = "Beijing" select C; MessageBox. show (queries. first (). cityname + queries. first (). citycode );}
A brief description of the query statement:
2. iqueryable <cityinfotable> queries = from C in dB. cityinfos where c. province = prov select C; -- iqueryable indicates the query; cityinfotable indicates the table structure; C indicates the fixed writing method, DB. cityinfos is the query table; where indicates the query condition 3.var queries = from C in dB. cityinfos where (C. province = Province & C. cityname = cityname) Select C; -- where (C. province = Province & C. cityname = cityname) Multiple query Conditions

 

Related Article

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.