C# Winform 里面读取 XML 的方法(转)
写 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");
{
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;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)