C# winform中如何使用xml保存和加载文件 我在一个form中放了panel,button两个控件,现在我想使用xml文件来保存整个文件,读取xml的时候还能把整个文件还原成panel,button两个控件,该如何写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Xml;namespace xml{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { XmlDocument xmldoc = new XmlDocument(); //加入XML的声明段落 XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", ""); xmldoc.AppendChild(xmlnode); //加入一个根元素 XmlElement root = xmldoc.CreateElement("", "root", ""); xmldoc.AppendChild(root); XmlElement ej = xmldoc.CreateElement("name"); XmlElement ej1 = xmldoc.CreateElement("xname"); ej1.InnerText = "bb"; XmlElement dj = xmldoc.CreateElement("sex"); dj.InnerText = "男"; ej.AppendChild(ej1); root.AppendChild(ej); root.AppendChild(dj); xmldoc.Save(@"d:\x.xml"); } }} 类似word软件 可以保存一个里面有文字又有图片的文件,还可以加载显示出来 读取/写入 XML文件,楼主去看XMLDocument/XMLReader/XMLWriter就知道怎么写了至于保存Panel/Button,主要是看你想保存什么信息了,比如Button的大小,Text等,就可以像下面这么写:<Button> <Text>button</Text> <Width>100</Width> ..........</Button> 这种资料难找?XML 读取 几百万张网页,xml保存好控件的属性,读出来后new一个新的控件对象,赋上相应属性,最后再把控件加到容器里,不就行了。xml的节点层次,节点名称,属性名称等自己定。 把form里的每个控件当成xml里的一个节点,把所需的控件的属性再写到相应的节点里就行了。 建议LZ看一下WPF,界面元素就是保存在XML文件的 http://blog.csdn.net/csshan/article/details/2056711 可以看看我曾写的 UP,UP 我现在已经将一个button保存到xml中了,接下来改如何从xml文件读取出来并在界面上显示? WPF中控件显示问题,Z轴显示不对,控件被遮住了 被问及一道缓存的题目,看有没有更加好的回答 ftp问题,着急 群组权限设定的问题? winform中2个datagrid问题 一条sql语句,请赐教 ASP运行出错 怎样在toolbar中显示每一个button中的hint内容 A question of caibird:) 有谁开发过QQ一类的软件,能给个方案或设计文档之类的吗? 为什么我调用新浪的短信接口出错? 这组数据存在一个变量中,我想把它按字段存入数据库中,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace xml
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ XmlDocument xmldoc = new XmlDocument();
//加入XML的声明段落
XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode);
//加入一个根元素
XmlElement root = xmldoc.CreateElement("", "root", "");
xmldoc.AppendChild(root);
XmlElement ej = xmldoc.CreateElement("name");
XmlElement ej1 = xmldoc.CreateElement("xname");
ej1.InnerText = "bb";
XmlElement dj = xmldoc.CreateElement("sex");
dj.InnerText = "男";
ej.AppendChild(ej1);
root.AppendChild(ej);
root.AppendChild(dj);
xmldoc.Save(@"d:\x.xml");
}
}
}
<Button>
<Text>button</Text>
<Width>100</Width>
..........
</Button>
XML 读取 几百万张网页,
xml保存好控件的属性,读出来后new一个新的控件对象,赋上相应属性,最后再把控件加到容器里,不就行了。xml的节点层次,节点名称,属性名称等自己定。