Windows Phone开发之XML解析和SQL CE数据库之初体验

提纲:
本文涉及的内容主要是有关本地XML的解析(同理,Web端的XML数据解析方法一样),然后将解析的XML数据保存到SQL CE数据库中。
SQL CE数据库是在程序中创建的,并保存在独立存储区域,因此在项目中是看不到的。

参考博文:
http://blog.csdn.net/fengyun1989/article/details/7342774

第一部分:XML解析
图图说明11:

1.确保项目中包含System.Xml.Ling和System.Xml的引用,没有引用的自己添加引用即可。
2.代码如下:

//读取资源文件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();
                    }
                    //解析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;
                            //把数据存入数据库
                            CityInfoTable cityInfo = new CityInfoTable(); 
       cityInfo.CityCode = cityid;
                            cityInfo.Province = province;
                            cityInfo.CityName = cityname;
                            db.CityInfos.InsertOnSubmit(cityInfo);

                        }
                    }

代码说明:
StreamResourceInfo用来解析resource资源,因此此步骤之前先将XML文件属性修改为Resource。
XDocument接收XML文档
XElement表示XML节点元素
doc.Descendants("root").Nodes()表示Root节点下的所有子节点
item.Attribute("data").Value获取节点data的值

以上部分就是XML解析的步骤,解析之前只需要知道对应的节点的名称即可。

=============================================
第2部分:SQL CE的使用(创建数据库和查询数据)
1.首先添加数据库需要的System.data.ling引用
2.创建数据库表的结构
建立表的结构: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; }
        }
    }
}

代码说明:
[Table]表示类CityInfoTable为一张表,必须包含此字段,不然创建数据库时会有表不存在的错误。
_cityInfoId,_province,_cityName,_cityCode表示表的4个字段.
[Column]表示是数据库表中一列,[Column(....)]表示该列存在哪些属性性质.

3.创建数据库表
CityDataContext.cs文件用来创建数据库:

 public class CityDataContext:DataContext
    {
        //定义数据库连接字符串
        public static string connectionString = "Data Source=isostore:/CityInfo.sdf";

         传递数据库连接字符串到DataContext基类
        public CityDataContext(string connectionString) : base(connectionString) { }

        //定义一个数据表,如果有多个数据表也可以继续添加为成员变量
        public Table<CityInfoTable> CityInfos
        {
            get
            {
                return this.GetTable<CityInfoTable>();
            }
        }

    }

代码说明:
CityDataContext继承自DataContext。
connectionString作为数据库连接字符串,存储于独立存储区域的CityInfo.sdf文件中。
CityInfos为数据库表的名称。
4.添加数据

private void CreateDB()
        {
            using (CityDataContext db = new CityDataContext(CityDataContext.connectionString))
            {
                if (db.DatabaseExists()==false)
                {
                    //创建数据库
                    db.CreateDatabase();
                    //读取资源文件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();
                    }
                    //解析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;
                            //把数据存入数据库
                            CityInfoTable cityInfo = new CityInfoTable(); 
       cityInfo.CityCode = cityid;
                            cityInfo.Province = province;
                            cityInfo.CityName = cityname;
                            db.CityInfos.InsertOnSubmit(cityInfo);

                        }
                    }
                    //数据库提交更新
                    db.SubmitChanges();
                }
            }
        }

代码说明:
  CityInfoTable cityInfo = new CityInfoTable();
 cityInfo.CityCode = cityid;
    cityInfo.Province = province;
    cityInfo.CityName = cityname;
  db.CityInfos.InsertOnSubmit(cityInfo);
  cityInfo为一张创建的数据表,添加数据后,添加到CityInfos数据库表中,并提交。
  -------------------------------------
补充查询语句:
  查询北京市的城市信息,并弹窗显示出来。

  1. using (CityDataContext db = new CityDataContext(CityDataContext.connectionString))
                    {
                    IQueryable<CityInfoTable> queries =  from c in db.CityInfos where c.Province == "北京" && c.CityName == "北京" select c;
                    MessageBox.Show(queries.First().CityName + queries.First().CityCode);


}
查询语句简单说明:
2.IQueryable<CityInfoTable> queries = from c in db.CityInfos where c.Province == prov select c;
--IQueryable表示查询;CityInfoTable是表结构;c为固定写法,db.CityInfos为查询的表;where表示查询条件

3.var queries = from c in db.CityInfos where (c.Province == province && c.CityName == cityName) select c;
--where (c.Province == province && c.CityName == cityName)为多条查询条件



 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值