1.创建xml文件
public void WriteXml()
{
XmlDocument xmlDoc = new XmlDocument();
//创建类型声明
xmlDoc = new XmlDocument();
XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
xmlDoc.AppendChild(node);
//创建父节点
XmlNode root = xmlDoc.CreateElement("Camera");
xmlDoc.AppendChild(root);
//子节点1
XmlNode node1 = xmlDoc.CreateNode(XmlNodeType.Element, "originalPath", null);
CreateNode(xmlDoc, node1, "name", textBox1.Text);
root.AppendChild(node1);
//子节点2
XmlNode node2 = xmlDoc.CreateNode(XmlNodeType.Element, "compoundPath", null);
CreateNode(xmlDoc, node2, "name", textBox2.Text);
root.AppendChild(node2);
//保存
xmlDoc.Save(Application.StartupPath + "//配置文件//Camera.xml");
}
//写入xml node子节点属性
public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
{
XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
node.InnerText = value;
parentNode.AppendChild(node);
}
2.读取xml文件内容
public List<string> ReadXml(string param)
{
XmlDocument doc = new XmlDocument();
doc.Load(XMLPath);
//获取根节点
XmlElement root = doc.DocumentElement;
XmlNode nodeParent = root.SelectSingleNode(param);
XmlNodeList nodes = nodeParent.ChildNodes;
//获取值
List<string> strs = new List<string>();
foreach (XmlNode node in nodes)
{
strs.Add(node.InnerText);
}
return strs;
}
3.xml文件样式
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a18cbfb039fb95694e4992732ed00c45.png#pic_center)