当前位置:网站首页>c#操作xml文件
c#操作xml文件
2022-07-05 11:23:00 【全栈程序员站长】
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml;
namespace delegatedemo { public class XmlClass { private string xmlPath; public XmlClass(string xmlPath) { this.xmlPath = xmlPath; }
public DataView GetXmlElement() { DataTable myTable = new DataTable(); DataRow myRow; myTable.Columns.Add(“学号”); myTable.Columns.Add(“姓名”); myTable.Columns.Add(“年龄”); myTable.Columns.Add(“性别”); XmlDocument doc = new XmlDocument(); doc.Load(xmlPath); XmlElement xmlElement = doc.DocumentElement; foreach (XmlNode node in xmlElement.ChildNodes) { string number = node.Attributes[“number”].Value; string age = node.SelectSingleNode(“Age”).InnerText; string name = node.SelectSingleNode(“Name”).InnerText; string sex = node.SelectSingleNode(“Sex”).InnerText; myRow = myTable.NewRow(); myRow[“学号”] = number; myRow[“姓名”] = name; myRow[“性别”] = sex; myRow[“年龄”] = age; myTable.Rows.Add(myRow); } return myTable.DefaultView; }
public void InsertXml() { XmlDocument doc = new XmlDocument(); doc.Load(xmlPath); XmlElement root = doc.DocumentElement; XmlElement student = doc.CreateElement(“student”); student.SetAttribute(“number”, “003”); XmlElement name = doc.CreateElement(“name”); name.InnerText = “张三”; XmlElement age = doc.CreateElement(“age”); age.InnerText = “21”; XmlElement sex = doc.CreateElement(“sex”); sex.InnerText = “女”; root.AppendChild(student); root.AppendChild(name); root.AppendChild(age); root.AppendChild(sex); doc.Save(xmlPath); } } }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/109546.html原文链接:https://javaforall.cn
边栏推荐
- [advertising system] incremental training & feature access / feature elimination
- 7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
- 使用GBase 8c数据库过程中报错:80000305,Host ips belong to different cluster ,怎么解决?
- Risc-v-qemu-virt in FreeRTOS_ Scheduling opportunity of GCC
- Intelligent metal detector based on openharmony
- Home office things community essay
- [TCP] TCP connection status JSON output on the server
- 【Office】Excel中IF函数的8种用法
- Deepfake tutorial
- 边缘计算如何与物联网结合在一起?
猜你喜欢
Huawei equipment configures channel switching services without interruption
如何让全彩LED显示屏更加节能环保
Pytorch training process was interrupted
Stop saying that microservices can solve all problems!
About the use of Vray 5.2 (self research notes) (II)
【爬虫】wasm遇到的bug
[JS] extract the scores in the string, calculate the average score after summarizing, compare with each score, and output
Lombok 同时使⽤@Data和@Builder 的坑,你中招没?
【Oracle】使用DataGrip连接Oracle数据库
7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见
随机推荐
C#实现WinForm DataGridView控件支持叠加数据绑定
数据库三大范式
About the use of Vray 5.2 (self research notes)
7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见
Question bank and answers of special operation certificate examination for main principals of hazardous chemical business units in 2022
Array
[advertising system] parameter server distributed training
Wechat nucleic acid detection appointment applet system graduation design completion (7) Interim inspection report
【Office】Excel中IF函数的8种用法
How to close the log window in vray5.2
COMSOL--三维随便画--扫掠
MFC pet store information management system
【全网首发】(大表小技巧)有时候 2 小时的 SQL 操作,可能只要 1 分钟
String
[Oracle] use DataGrid to connect to Oracle Database
力扣(LeetCode)185. 部门工资前三高的所有员工(2022.07.04)
R3live series learning (IV) r2live source code reading (2)
Applet framework taro
以交互方式安装ESXi 6.0
Dspic33ep clock initialization program