c#读写xml小问题 1.怎么创建2.怎么把"123"这个字符串存进去3.怎么把"123"读取出来4.怎么遍历所有存进去的字符串5.能不能存入Bitmap等..谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.csdn.net/mengxj85/archive/2008/09/30/3005557.aspx 这些问题楼主应该去看教程啊,VS2008还可以用linq来做你说的那几个 你问的这个问题面太大,没法回答。请先看看xml基础吧。1,创建xml方法也很多,一般采用FileStream的Write方法创建较多2,3面太广,没法回答4,LINQ TO XML或将xml读入数据集然后遍历5,不明白什么意思 http://www.alixixi.com/program/a/2008050727580.shtml再来一个更详细的教程这种问题最好先百度,然后再来问,这样大家回答才有目的性 System.Xml.XmlDocument doc = new XmlDocument();XmlNode root =doc.CreateElement("Setting"); 晕C# 建立xml文件的方法 string xmlFile = System.Windows.Forms.Application.StartupPath + "\\system.xml"; XmlDocument xml = new XmlDocument(); //建立XmlDomcument对象 XmlDeclaration Declaration = xml.CreateXmlDeclaration("1.0", "utf-8", null); //Xml Declaration(Xml声明) XmlNode RootNode = xml.CreateNode(XmlNodeType.Element, "", "ROOT", ""); xml.AppendChild(RootNode); XmlNode node1 = xml.CreateNode(XmlNodeType.Element, "", "频率范围", ""); node1.Attributes.Append(xml.CreateAttribute("VALUE")).InnerText = 123; xml.InsertBefore(Declaration, xml.DocumentElement); try { xml.Normalize(); xml.Save(xmlFile); } catch (Exception ex) { // 显示错误信息 MessageBox.Show("保存xml文件出错:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }读: XmlDocument xml = new XmlDocument(); if (File.Exists(System.Windows.Forms.Application.StartupPath + "\\system.xml")) { xml.Load(System.Windows.Forms.Application.StartupPath + "\\system.xml"); XmlNode root = xml.DocumentElement;//读取XML的根节点 foreach (XmlNode node in root.ChildNodes)//对子节点进行循环 { if (node.HasChildNodes) { string str = wavecomboBox.Text.Substring(0, wavecomboBox.Text.Length - 2); if (node.Attributes["VALUE"].Value == str) { foreach (XmlNode childnode in node.ChildNodes) { if (childnode.Name == "霍尔") { choicehall = int.Parse(childnode.InnerText); } else if (childnode.Name == "温度") { choiceTemp = int.Parse(childnode.InnerText) * 10; } else if (childnode.Name == "LT") { choiceLTVolt = int.Parse(childnode.InnerText); } } } } } } else { MessageBox.Show("配置文件未找到,请确认程序文件夹下是否存在“System.xml”这个文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } XML成为可扩展性标记性语言,在.NET框架中可以说是非常重要的一部分,它用于描述数据。在该项目中添加XML文件XML文件是以节点的形式存在的,并且区分大小写,描述的每个数据的各个节点都可以自由的扩展。<student><name>jack</name><age>22</age><name>rose</name><age>21</age></student>读取:要想操作XML文件,需要引入System.XML命名空间1、实例化XmlDocument对象,该对象可标识整个XML文档 2、利用它的Load()方法将指定的XML文档加载如该XmlDocument对象3、获取该文档的根节点 节点对象为XmlNode4、遍历循环其下子节点XmlDocument myxml = new XmlDocument();myxml.Load("指定XML文件");XmlNode node = myxml.DocumentElement;foreach(XmlNode demoNode in node.ChildNodes){ switch(demoNode.Name) { //执行代码 }} C#中画等高线的控件 WCF学习,配置文件 关于在C#里面直接执行sql查询语句误差! 使用webBrowser做的浏览器 安装activex插件问题 求一个方法越简单越好马上结贴。。。 update和delete触发 有没有 类似票据上小写 金额栏的控件 关于读取XML 如何用file field上传图片时,把图片写入数据库,又把图片读出 奇怪的问题:鼠标点击选择不到控件 C# 用户自定义控件添加时出现意想不到结果 请多多指教 正则式有关问题:这个正则式前面的(?m)什么含义
2,3面太广,没法回答
4,LINQ TO XML或将xml读入数据集然后遍历
5,不明白什么意思
XmlNode root =doc.CreateElement("Setting");
C# 建立xml文件的方法
string xmlFile = System.Windows.Forms.Application.StartupPath + "\\system.xml"; XmlDocument xml = new XmlDocument(); //建立XmlDomcument对象
XmlDeclaration Declaration = xml.CreateXmlDeclaration("1.0", "utf-8", null); //Xml Declaration(Xml声明)
XmlNode RootNode = xml.CreateNode(XmlNodeType.Element, "", "ROOT", "");
xml.AppendChild(RootNode); XmlNode node1 = xml.CreateNode(XmlNodeType.Element, "", "频率范围", "");
node1.Attributes.Append(xml.CreateAttribute("VALUE")).InnerText = 123;
xml.InsertBefore(Declaration, xml.DocumentElement);
try
{
xml.Normalize();
xml.Save(xmlFile);
}
catch (Exception ex)
{
// 显示错误信息
MessageBox.Show("保存xml文件出错:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
读: XmlDocument xml = new XmlDocument();
if (File.Exists(System.Windows.Forms.Application.StartupPath + "\\system.xml"))
{
xml.Load(System.Windows.Forms.Application.StartupPath + "\\system.xml");
XmlNode root = xml.DocumentElement;//读取XML的根节点
foreach (XmlNode node in root.ChildNodes)//对子节点进行循环
{
if (node.HasChildNodes)
{
string str = wavecomboBox.Text.Substring(0, wavecomboBox.Text.Length - 2);
if (node.Attributes["VALUE"].Value == str)
{
foreach (XmlNode childnode in node.ChildNodes)
{
if (childnode.Name == "霍尔")
{
choicehall = int.Parse(childnode.InnerText);
}
else if (childnode.Name == "温度")
{
choiceTemp = int.Parse(childnode.InnerText) * 10;
}
else if (childnode.Name == "LT")
{
choiceLTVolt = int.Parse(childnode.InnerText);
}
}
}
}
}
}
else
{
MessageBox.Show("配置文件未找到,请确认程序文件夹下是否存在“System.xml”这个文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
<student>
<name>jack</name>
<age>22</age>
<name>rose</name>
<age>21</age>
</student>
读取:
要想操作XML文件,需要引入System.XML命名空间1、实例化XmlDocument对象,该对象可标识整个XML文档
2、利用它的Load()方法将指定的XML文档加载如该XmlDocument对象
3、获取该文档的根节点 节点对象为XmlNode
4、遍历循环其下子节点
XmlDocument myxml = new XmlDocument();
myxml.Load("指定XML文件");
XmlNode node = myxml.DocumentElement;foreach(XmlNode demoNode in node.ChildNodes)
{
switch(demoNode.Name)
{ //执行代码
}
}