C# WinForm 读写XML文件

C# WinForm 读写XML文件,第1张

建立一个WinForm应用程序 添加MenuStrip控件 填写两个功能 读取 和 导出数据

用了两个DataSet控件和对话框 打开(OpenFilesDialog控件) 和 保存(SaveFilesDialog控件)

读取                  private void 读取ToolStripMenuItem_Click(object sender EventArgs e)                {                     if (opFileDlg ShowDialog() == DialogResult OK)                    {                      竖洞腊   if(opFileDlg OpenFile()!=null)        颤念                 {                             oXML ReadXml (@opFileDlg FileName )                             foreach (DataRow oRow in oXML Tables [ user ] Rows)                             {                                DataRow newRow = dsXML Tables[ user ] NewRow()                                newRow [ 序号 ]  = oRow [ 序号 ]                                newRow[ 标题 ] = oRow[ 标题 ]                                newRow[ 网址 ] = oRow[ 网址 ]                             余滑   newRow[ 用户名 ] = oRow[ 用户名 ]                                newRow[ 密码 ] = oRow[ 密码 ]                                newRow[ 时间 ] = oRow[ 时间 ]                                newRow[ 备注 ] = oRow[ 备注 ]                                dsXML Tables [ user ] Rows Add(newRow)                             }                             int n = dsXML Tables [ user ] Rows Count                              for(int i= i<ni++)                             {                                 dsXML Tables [ user ] Rows [i][ 序号 ]=i+                              }                             dsXML WriteXml(@ user xml )                             this Visible = true                             MessageBox Show( 数据导入成功! 成功 )                         }                    }                    else                    {                       this Visible  = true                    }                }        导出

private void 导出ToolStripMenuItem_Click(object sender EventArgs e)                {                    if (svFileDlg ShowDialog() == DialogResult OK)                    {                        dsXML WriteXml(@svFileDlg FileName)                        this Visible = true                        MessageBox Show( 数据导出成功! 成功 )                    }                    else                    {                       this Visible  = true                    }                }

lishixinzhi/Article/program/ASP/201311/21826

C#中对XML文件的基本 *** 作

xml文件格式如下:

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

<userdata createuser="false">

<dataconnection>

<server>localhost</server>

<uid>sa</uid>

<pwd></pwd>

</dataconnection>

<net>

<name>jiayuan</name>

</net>辩睁

</userdata>

读取节点中的一个虚袜属性

XmlDocument doc=new XmlDocument()

doc.Load("config.xml")

XmlNode xnuser=doc.SelectSingleNode("userdata")

string flag=xnuser.Attributes["createuser"].InnerText

读取节点中的值

XmlDocument doc=new XmlDocument()

doc.Load("config.xml")

XmlNode xnserver = doc.SelectSingleNode("userdata/dataconnection/server")

修改节点的属性

XmlDocument doc=new XmlDocument()

doc.Load("config.xml")

XmlNode xnuser=doc.SelectSingleNode("userdata")

xnuser.Attributes["createuser"].InnerText="false"

doc.Save("config.xml")

追加节点差灶激

XmlDocument doc = new XmlDocument()

XmlTextReader reader = new XmlTextReader("config.xml")

doc.Load("config.xml")

XmlElement root = doc.DocumentElement// 获取根节点

XmlElement tagMessage = doc.CreateElement("net")

XmlElement tagText = doc.CreateElement("name")

tagText.InnerText = netname

tagMessage.AppendChild(tagText) // 追加到 xml 文本的最后面

root.AppendChild(tagMessage)

reader.Close()// 关闭 XmlTextReader

doc.Save("config.xml")// 保存 xml 文件


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12225640.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存