博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# Winform 里面读取 XML 的方法(转)
阅读量:6939 次
发布时间:2019-06-27

本文共 2747 字,大约阅读时间需要 9 分钟。

写 XML 文件的

//public static void SetXmlFileValue(string xmlPath, string AppKey, string AppValue)//写xmlPath是文件路径+文件名,AppKey是 Key Name,AppValue是Value
public static void SetXmlFileValue(string xmlPath, string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlPath);
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings";
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']";
if (xElem1 != null)
{
xElem1.SetAttribute("value", AppValue);
}
else
{
xElem2 = xDoc.CreateElement("add";
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(xmlPath);
}
读取 XML 文件节点内容
//public static void GetXmlFileValue(string xmlPath, string AppKey, ref string AppValue)//读xmlPath是文件路径+文件名,AppKey是 Key Name,AppValue是Value
public static string GetXmlFileValue(string xmlPath, string AppKey)
{
string strValue = "";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlPath);
XmlNode xNode;
XmlElement xElem1;
xNode = xDoc.SelectSingleNode("//appSettings";
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']";
if (xElem1 != null)
{
strValue = xElem1.GetAttribute("value";
}
else
{
// MessageBox.Show ("There is not any information!";
}
return strValue;
}

<?xml version="1.0" encoding="utf-8"?>

<System.Config>
<appSettings>
<add key="Server" value="D085D536F765EEB74123E527CEC0F564" />
<add key="DataBase" value="9323125653A3D08C2C1BF16192A2A2B8" />
<add key="User" value="7DDB8369CD879AE4" />
<add key="Password" value="B46F3E9E2A88B035" />
<!--Export to Excel,Chinese will become wrong code format at utf-8-->
<add key="ExportExcelEncoding" value="gb2312" />
</appSettings>
</System.Config>

代码里面的调用方法
读取:GetXmlFileValue(strXmlPath, "Server");
写入:SetXmlFileValue("setup.xml", "Server", "value");

private void 读取ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (opFileDlg .ShowDialog() == DialogResult.OK)
{
if(opFileDlg .OpenFile()!=null)
{
twoXML .ReadXml (@opFileDlg .FileName );
foreach (DataRow twoRow in twoXML .Tables ["user"].Rows)
{
DataRow newRow = dsXML.Tables["user"].NewRow();
newRow ["序号"] = twoRow ["序号"];
newRow["标题"] = twoRow["标题"];
newRow["网址"] = twoRow["网址"];
newRow["用户名"] = twoRow["用户名"];
newRow["密码"] = twoRow["密码"];
newRow["时间"] = twoRow["时间"];
newRow["备注"] = twoRow["备注"];
dsXML .Tables ["user"].Rows .Add(newRow);
}
int n = dsXML .Tables ["user"].Rows .Count ;
for(int i=0;i<n;i++)
{
dsXML .Tables ["user"].Rows [i]["序号"]=i+1;
}
dsXML.WriteXml(@"user.xml");
this.Visible = true;
MessageBox.Show("数据导入成功!", "成功"); 中国网管联盟www、bitsCN、com
}
}
else
{
this.Visible = true;
}
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/11/27/1611876.html,如需转载请自行联系原作者
你可能感兴趣的文章
ecmall用户登陆成功,自动退出问题的解决方法
查看>>
Spring 之 示例(Java之负基础实战)
查看>>
关于H5页面在iPhoneX适配
查看>>
UI整理-----part6--页面间的传值
查看>>
编程之美:寻找发帖"水王"
查看>>
eclips的一些快捷设置
查看>>
POJ 1769 Minimizing maximizer 线段树优化DP
查看>>
mvc 验证封装到某个特征类里[特性的使用]
查看>>
Redis配置文件详解
查看>>
SQL Mon 介绍
查看>>
yii2 对字段 自动加一 或 减一
查看>>
nginx负载均衡分配策略有哪些?
查看>>
localStorage
查看>>
简单的Asp.net mvc 里动态生成Linq的Ef查询扩展
查看>>
java.lang.reflect.InvocationTargetException
查看>>
完善(用户调研反馈+自评+典型用户与场景)
查看>>
elasticsearch接口开发(新)
查看>>
How to configure your MyInbox webpart automatically ?
查看>>
Linux : centOS 与 Ubuntu 安装 Nginx
查看>>
Django&Admin站点&调整站点信息
查看>>