当前位置:网站首页>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
边栏推荐
- Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
- 7.2 daily study 4
- COMSOL--三维图形的建立
- Characteristics and electrical parameters of DDR4
- 跨境电商是啥意思?主要是做什么的?业务模式有哪些?
- Evolution of multi-objective sorting model for classified tab commodity flow
- 9、 Disk management
- 龙蜥社区第九次运营委员会会议顺利召开
- Redis如何实现多可用区?
- LSTM applied to MNIST dataset classification (compared with CNN)
猜你喜欢

Evolution of multi-objective sorting model for classified tab commodity flow

R3live series learning (IV) r2live source code reading (2)

Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things

Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?

The ninth Operation Committee meeting of dragon lizard community was successfully held

How did the situation that NFT trading market mainly uses eth standard for trading come into being?

Repair animation 1K to 8K

7.2每日学习4

Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment

About the use of Vray 5.2 (self research notes) (II)
随机推荐
Cron expression (seven subexpressions)
Array
分类TAB商品流多目标排序模型的演进
居家办公那些事|社区征文
Paradigm in database: first paradigm, second paradigm, third paradigm
Spark Tuning (I): from HQL to code
spark调优(一):从hql转向代码
Data types ntext and varchar are incompatible in the not equal to operator - 95 small pang
R3Live系列学习(四)R2Live源码阅读(2)
Function///
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
购买小间距LED显示屏的三个建议
AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
A mining of edu certificate station
[SWT component] content scrolledcomposite
shell脚本文件遍历 str转数组 字符串拼接
Detailed explanation of MATLAB cov function
OneForAll安装使用
uniapp