我不想使用appconfig 因为那个太2 不能多个project 共用一个appconfig
所以我想在一个solution下用一个xml记录所有solution下project的配置信息。
有以下三个问题
不知这个xml应该怎么写?其次 我应该怎么读取和写入这个xml? 如果有不定长数组想存储在xml文件中,应该如何读写?问题很多
如果有样例程序更好
所以我想在一个solution下用一个xml记录所有solution下project的配置信息。
有以下三个问题
不知这个xml应该怎么写?其次 我应该怎么读取和写入这个xml? 如果有不定长数组想存储在xml文件中,应该如何读写?问题很多
如果有样例程序更好
访问方式很简单,XML Document搞定,对你来说肯定没问题!
<persons>
<person name="张三" age="20" sex="男"/>
<person name="李四" age="21" sex="女"/>
</persons>
<persons>
<person>
<name>张三</name>
<age>20</age>
<sex>男</sex>
</person>
<person>
<name>李四</name>
<age>21</age>
<sex>女</sex>
</person>
</persons>
using System.Xml;XmlReader reader = new XmlReader.Create("books.xml");
while (reader.Read())
{
if (reader.NodeType = XmlNodeType.Text)
{
XXXXX;
}
else if (reader.NodeType = XmlNodeType.Element)
{
XXXXX;
}
}
跟多的示例你可以搜索MSDN System.Xml中得到
mDocument = new XmlDocument();
pictureBox1.BackColor = System.Drawing.Color.Red;
try
{
mDocument.Load(@"ModbusIni.xml");//判断文件Load正确否
}
catch
{
// 走到这里就失败了
bSave = true;
CreatXmlDocument();
pictureBox1.BackColor = System.Drawing.Color.Yellow;
}
if (!bSave)
{
ReadXmlDocument(); }
private void CreatXmlDocument() //建立新的文件
{
//定义XML文档头文件
XmlDeclaration xmlDeclaration = mDocument.CreateXmlDeclaration("1.0", "utf-8", null);
//增加XML文档头
mDocument.AppendChild(xmlDeclaration); //定义XML的根
XmlElement xmlRoot = mDocument.CreateElement("Modbus");
//添加XML的根
mDocument.AppendChild(xmlRoot); //添加根的属性
xmlRoot.SetAttribute("Version", "1.00.00");
XmlElement newCommPort = mDocument.CreateElement("CommPort");
XmlElement newTAddress = mDocument.CreateElement("TAddress"); XmlText CommPort = mDocument.CreateTextNode("COM1");
XmlText TAddress = mDocument.CreateTextNode("0"); newCommPort.AppendChild(CommPort);
newTAddress.AppendChild(TAddress); //添加XML根的节点
xmlRoot.AppendChild(newCommPort);
xmlRoot.AppendChild(newTAddress); // Save the document to a file.
mDocument.Save("ModbusIni.xml");
comboBox1.Text = CommPort.InnerText;
comboBox2.Text = TAddress.InnerText;
}
private void ReadXmlDocument() //读取Xml文件
{
XmlElement xmlRoot = mDocument.DocumentElement;
mCurrentNode = xmlRoot.SelectSingleNode("CommPort");
comboBox1.Text = mCurrentNode.InnerText;
mCurrentNode = xmlRoot.SelectSingleNode("TAddress");
comboBox2.Text = mCurrentNode.InnerText; }
private void SaveXmlDocument() //保存Xml文件
{
XmlElement xmlRoot = mDocument.DocumentElement;
mCurrentNode = xmlRoot.SelectSingleNode("CommPort");
mCurrentNode.InnerText = comboBox1.Text;
mCurrentNode = xmlRoot.SelectSingleNode("TAddress");
mCurrentNode.InnerText = comboBox2.Text; mDocument.Save("ModbusIni.xml");
}
[/code]
mDocument = new XmlDocument();
pictureBox1.BackColor = System.Drawing.Color.Red;
try
{
mDocument.Load(@"ModbusIni.xml");//判断文件Load正确否
}
catch
{
// 走到这里就失败了
bSave = true;
CreatXmlDocument();
pictureBox1.BackColor = System.Drawing.Color.Yellow;
}
if (!bSave)
{
ReadXmlDocument(); }
private void CreatXmlDocument() //建立新的文件
{
//定义XML文档头文件
XmlDeclaration xmlDeclaration = mDocument.CreateXmlDeclaration("1.0", "utf-8", null);
//增加XML文档头
mDocument.AppendChild(xmlDeclaration); //定义XML的根
XmlElement xmlRoot = mDocument.CreateElement("Modbus");
//添加XML的根
mDocument.AppendChild(xmlRoot); //添加根的属性
xmlRoot.SetAttribute("Version", "1.00.00");
XmlElement newCommPort = mDocument.CreateElement("CommPort");
XmlElement newTAddress = mDocument.CreateElement("TAddress"); XmlText CommPort = mDocument.CreateTextNode("COM1");
XmlText TAddress = mDocument.CreateTextNode("0"); newCommPort.AppendChild(CommPort);
newTAddress.AppendChild(TAddress); //添加XML根的节点
xmlRoot.AppendChild(newCommPort);
xmlRoot.AppendChild(newTAddress); // Save the document to a file.
mDocument.Save("ModbusIni.xml");
comboBox1.Text = CommPort.InnerText;
comboBox2.Text = TAddress.InnerText;
}
private void ReadXmlDocument() //读取Xml文件
{
XmlElement xmlRoot = mDocument.DocumentElement;
mCurrentNode = xmlRoot.SelectSingleNode("CommPort");
comboBox1.Text = mCurrentNode.InnerText;
mCurrentNode = xmlRoot.SelectSingleNode("TAddress");
comboBox2.Text = mCurrentNode.InnerText; }
private void SaveXmlDocument() //保存Xml文件
{
XmlElement xmlRoot = mDocument.DocumentElement;
mCurrentNode = xmlRoot.SelectSingleNode("CommPort");
mCurrentNode.InnerText = comboBox1.Text;
mCurrentNode = xmlRoot.SelectSingleNode("TAddress");
mCurrentNode.InnerText = comboBox2.Text; mDocument.Save("ModbusIni.xml");
}
我想的是同一个solution中必定有很多工程配置相同 所以通用一个xml 每个工程独立的参数再单独一个xml