资源简介
wince下的C#读取xml文件,wince智能项目不支持相对路径,所以没有Setting.setting配置文件,所以需要读写xml,下载文件中有详细说明
代码片段和文件信息
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.xml;
namespace SmartDevicexmlTest
{
public partial class Form1 : Form
{
string filename = ““;
public Form1()
{
InitializeComponent();
}
private void btWrite_Click(object sender EventArgs e)
{
xmlDocument xmlDoc = new xmlDocument();
xmlDoc.Load(filename);
//xmlDoc.Loadxml(“ “);
xmlNode root = xmlDoc.SelectSingleNode(“bookstore“);//查找
xmlElement xe1 = xmlDoc.CreateElement(“book“);//创建一个 节点
xe1.SetAttribute(“genre“ “李赞红“);//设置该节点genre属性
xe1.SetAttribute(“ISBN“ “2-3631-4“);//设置该节点ISBN属性
xmlElement xesub1 = xmlDoc.CreateElement(“title“);
xesub1.InnerText = “CS从入门到精通“;//设置文本节点
xe1.AppendChild(xesub1);//添加到 节点中
xmlElement xesub2 = xmlDoc.CreateElement(“author“);
xesub2.InnerText = “候捷“;
xe1.AppendChild(xesub2);
xmlElement xesub3 = xmlDoc.CreateElement(“price“);
xesub3.InnerText = “58.3“;
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到 节点中
xmlDoc.Save(filename);
//插入完成后读取信息
//ShowAllInfo(filename xmlDoc);
}
private void btRead_Click(object sender EventArgs e)
{
xmlDocument xmlDoc = new xmlDocument();
xmlDoc.Load(filename);
ShowAllInfo(filename xmlDoc);
//修改完成后读取信息
}
private void ShowAllInfo(string tmpfilename xmlDocument tmpxmlDoc)
{
textBox1.Text = ““;
//xmlNode xn = tmpxmlDoc.SelectSingleNode(“bookstore“);
//xmlNodeList xnl = xn.ChildNodes;
xmlNodeList xnl = tmpxmlDoc.SelectSingleNode(“bookstore“).ChildNodes;
foreach (xmlNode xnf in xnl)
{
xmlElement xe = (xmlElement)xnf;
//Console.WriteLine(xe.GetAttribute(“genre“));//显示属性值
//Console.WriteLine(xe.GetAttribute(“ISBN“));
textBox1.Text += xe.GetAttribute(“genre“) + “\r\n“;
textBox1.Text += xe.GetAttribute(“ISBN“) + “\r\n“;
xmlNodeList xnf1 = xe.ChildNodes;
foreach (xmlNode xn2 in xnf1)
{
//Console.WriteLine(xn2.InnerText);//显示子节点点文本
textBox1.Text += xn2.InnerText+“\r\n“;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 516 2009-09-18 09:52 wince C# 读写xm
文件 10240 2011-12-22 15:04 wince C# 读写xm
文件 28160 2011-12-22 15:04 wince C# 读写xm
文件 219 2009-09-18 09:36 wince C# 读写xm
文件 5780 2011-12-22 15:04 wince C# 读写xm
文件 4398 2009-09-18 13:25 wince C# 读写xm
文件 6101 2009-09-18 13:25 wince C# 读写xm
文件 1882 2011-12-22 14:30 wince C# 读写xm
文件 847 2009-09-18 13:30 wince C# 读写xm
文件 10240 2011-12-22 15:04 wince C# 读写xm
文件 180 2011-12-22 14:32 wince C# 读写xm
文件 28160 2011-12-22 15:04 wince C# 读写xm
文件 180 2011-12-22 14:32 wince C# 读写xm
文件 383 2009-09-18 09:27 wince C# 读写xm
文件 1453 2009-09-18 09:27 wince C# 读写xm
文件 2373 2009-09-18 09:27 wince C# 读写xm
文件 5618 2009-09-18 09:27 wince C# 读写xm
文件 4202 2009-09-18 09:36 wince C# 读写xm
文件 198 2011-12-22 14:32 wince C# 读写xm
文件 1110 2009-09-18 09:27 wince C# 读写xm
..A..H. 17920 2011-12-22 15:19 wince C# 读写xm
文件 55 2011-12-22 15:20 wince C# 读写xm
目录 0 2011-12-27 11:41 wince C# 读写xm
目录 0 2011-12-27 11:41 wince C# 读写xm
目录 0 2011-12-27 11:41 wince C# 读写xm
目录 0 2011-12-27 11:41 wince C# 读写xm
目录 0 2011-12-27 11:41 wince C# 读写xm
目录 0 2011-12-27 11:41 wince C# 读写xm
目录 0 2011-12-27 11:41 wince C# 读写xm
目录 0 2011-12-27 11:41 wince C# 读写xm
............此处省略6个文件信息
评论
共有 条评论