c#写的Winforms程序,想要用xml操作一些数据想要在xml文档中写成下面的形式
<?xml version="1.0" encoding="utf-8" ?>
<NewNums>
<num key="1" value="13500740103"/>
</NewNums>
要怎么写?
读取的时候想存入Hashtable,所以也想通过key-value的形式读出来
要怎么读?
还有就是删除,因为数据只是为了防止突然断电而使之前录入的信息消失临时存储的,
所以我想在每次正常关闭程序的时候都删除这个临时的xml,每次运行时都检查是否有这个xml,有就读取,没有就创建
要怎么做?问题比较多,很重要,拜托大家了!
<?xml version="1.0" encoding="utf-8" ?>
<NewNums>
<num key="1" value="13500740103"/>
</NewNums>
要怎么写?
读取的时候想存入Hashtable,所以也想通过key-value的形式读出来
要怎么读?
还有就是删除,因为数据只是为了防止突然断电而使之前录入的信息消失临时存储的,
所以我想在每次正常关闭程序的时候都删除这个临时的xml,每次运行时都检查是否有这个xml,有就读取,没有就创建
要怎么做?问题比较多,很重要,拜托大家了!
增删改查都有了
http://blog.csdn.net/lovefootball/archive/2008/08/21/2785922.aspx
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<book>节点中
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);//添加到<bookstore>节点中
xmlDoc.Save("bookstore.xml");
你要的这里都有了.自己去看吧.
XmlNode node = doc.CreateNode("element", "NewNums", "");
doc.AppendChild(node);
XmlNode node2 = doc.CreateNode("element","num","");
doc.DocumentElement.AppendChild(node2);
XmlAttribute at = doc.CreateAttribute("key");
at.Value = "1";
XmlAttribute at2 = doc.CreateAttribute("value");
at2.Value = "13500740103";
node2.Attributes.Append(at);
node2.Attributes.Append(at2);
doc.Save(@"D:\Test For 2010\res\1.xml");
读:
Hashtable table = new Hashtable();
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\Test For 2010\res\1.xml");
XmlNodeList list = doc.DocumentElement.GetElementsByTagName("num");
for (int i = 0; i < list.Count; i++)
{
table.Add(list[0].Attributes["key"].Value, list[0].Attributes["value"].Value);
}delete : 文件删除,
删除之前判断,
if(File.Exists((@"D:\Test For 2010\res\1.xml"))
{}
delete : 文件删除,
删除之前判断,
if(File.Exists((@"D:\Test For 2010\res\1.xml"))
{
File.Delete(@"D:\Test For 2010\res\1.xml");}
路经自己修改
http://hi.baidu.com/wangweixp/blog/item/bca10dfa43732e9759ee90ff.html