pocketpc中嵌入xml文件

本文介绍了两种不同的XML数据读取方法:直接复制到PDA和作为嵌入资源使用。通过具体代码示例展示了如何实现这两种方法,并比较了它们在实际应用中的优缺点。

例如,我们需要读取users.xml中的数据,一种比较笨的方法是,将users.xml直接拷到pda上,但是这种方法并不通用,我们可以借鉴透明按钮的方法,把他作为嵌入的资源,这个所谓的嵌入的资源就是当部署的时候,会直接部署到pda上,不需要拷贝进去。

=======================================

第一种方法,我们直接拷到pda上:

=========================================

Cursor.Current = Cursors.WaitCursor;
DataSet ds = new DataSet();
ds.ReadXml((@"/users.xml"));//
这时我们把users.xml存放在/下
int count = ds.Tables["user"].Rows.Count;//看表中有多少个user
operateData.createFile("MobileLawEn.sdf");
if (ds != null && count > 0)
{
checkTableExistance("users");
SqlCeConnection conn = new SqlCeConnection(operateData.connStr);
SqlCeCommand cmd = new SqlCeCommand(
"create table users(username nvarchar(20),password nvarchar(20),deptid int,constraint p_key primary key(username,deptid))", conn);
conn.Open();
int rst = cmd.ExecuteNonQuery();
Cursor.Current = Cursors.Default;
for (int i = 0; i < count; i++)
{
string name = ds.Tables["user"].Rows[i]["username"].ToString();

//读取第i个user中的用户名

string password = ds.Tables["user"].Rows[i]["password"].ToString();
string deptid = ds.Tables["user"].Rows[i]["deptid"].ToString();
string sql = "insert into users (username,password,deptid) values ('" + name + "','" + password + "'," + deptid + ")";
SqlCeCommand comm = new SqlCeCommand(sql, conn);
comm.ExecuteNonQuery();
}

conn.Close();
Cursor.Current = Cursors.Default;

============================================

第二种方法:我们将xml文件作为嵌入的资源:

=============================================

首先嵌入:

pocketpc中如何嵌入xml文件

Cursor.Current = Cursors.WaitCursor;
DataSet ds = null;
ds = new DataSet();
System.IO.Stream strm = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileLawEn.xml.users.xml");

//GetManifestResourceStream是用来加载制定的资源。
ds.ReadXml(strm, System.Data.XmlReadMode.InferSchema);

//System.Data.XmlReadMode.InferSchema用来设置这种模式:

InferSchema

忽略任何嵌入式架构,并从 XML 数据推断架构

int count = ds.Tables["user"].Rows.Count;//看表中有多少个user
operateData.createFile("MobileLawEn.sdf");
if (ds != null && count > 0)
{
checkTableExistance("users");
SqlCeConnection conn = new SqlCeConnection(operateData.connStr);
SqlCeCommand cmd = new SqlCeCommand(
"create table users(username nvarchar(20),password nvarchar(20),deptid int,constraint p_key primary key(username,deptid))", conn);
conn.Open();
int rst = cmd.ExecuteNonQuery();
Cursor.Current = Cursors.Default;
for (int i = 0; i < count; i++)
{
string name = ds.Tables["user"].Rows[i]["username"].ToString();//读取第i个user中的用户名
string password = ds.Tables["user"].Rows[i]["password"].ToString();

//读取第i个user中的密码
string deptid = ds.Tables["user"].Rows[i]["deptid"].ToString();
string sql = "insert into users (username,password,deptid) values ('" + name + "','" + password + "'," + deptid + ")";
SqlCeCommand comm = new SqlCeCommand(sql, conn);
comm.ExecuteNonQuery();
}

conn.Close();
Cursor.Current = Cursors.Default;

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值