当前位置:网站首页>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
边栏推荐
- AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
- 解决grpc连接问题Dial成功状态为TransientFailure
- Leetcode 185 All employees with the top three highest wages in the Department (July 4, 2022)
- [SWT component] content scrolledcomposite
- [first release in the whole network] (tips for big tables) sometimes it takes only 1 minute for 2 hours of SQL operation
- 【全网首发】(大表小技巧)有时候 2 小时的 SQL 操作,可能只要 1 分钟
- Huawei equipment configures channel switching services without interruption
- MFC pet store information management system
- About the use of Vray 5.2 (self research notes) (II)
- Wechat nucleic acid detection appointment applet system graduation design completion (8) graduation design thesis template
猜你喜欢
Evolution of multi-objective sorting model for classified tab commodity flow
Question bank and answers of special operation certificate examination for main principals of hazardous chemical business units in 2022
matlab cov函数详解
【广告系统】增量训练 & 特征准入/特征淘汰
CDGA|数据治理不得不坚持的六个原则
COMSOL--三维随便画--扫掠
紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成
2022 mobile crane driver examination question bank and simulation examination
数据库三大范式
Three suggestions for purchasing small spacing LED display
随机推荐
Msfconsole command encyclopedia and instructions
go语言学习笔记-初识Go语言
BOM//
Process control
2022 mobile crane driver examination question bank and simulation examination
[SWT component] content scrolledcomposite
The art of communication III: Listening between people
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的调度时机
[JS learning notes 54] BFC mode
【爬虫】charles unknown错误
力扣(LeetCode)185. 部门工资前三高的所有员工(2022.07.04)
Summary of websites of app stores / APP markets
居家办公那些事|社区征文
The ninth Operation Committee meeting of dragon lizard community was successfully held
2022 t elevator repair operation certificate examination questions and answers
[there may be no default font]warning: imagettfbbox() [function.imagettfbbox]: invalid font filename
解决grpc连接问题Dial成功状态为TransientFailure
[Oracle] use DataGrid to connect to Oracle Database
POJ 3176-Cow Bowling(DP||记忆化搜索)
COMSOL--三维随便画--扫掠