当前位置:网站首页>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
边栏推荐
- 基于OpenHarmony的智能金属探测器
- AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
- 华为设备配置信道切换业务不中断
- 高校毕业求职难?“百日千万”网络招聘活动解决你的难题
- 【全网首发】(大表小技巧)有时候 2 小时的 SQL 操作,可能只要 1 分钟
- Bidirectional RNN and stacked bidirectional RNN
- 2022 t elevator repair operation certificate examination questions and answers
- 边缘计算如何与物联网结合在一起?
- What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
- 7.2 daily study 4
猜你喜欢
无密码身份验证如何保障用户隐私安全?
Detailed explanation of MATLAB cov function
AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
The ninth Operation Committee meeting of dragon lizard community was successfully held
Lombok 同时使⽤@Data和@Builder 的坑,你中招没?
Stop saying that microservices can solve all problems!
Three paradigms of database
紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成
About the use of Vray 5.2 (self research notes) (II)
7.2每日学习4
随机推荐
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Bidirectional RNN and stacked bidirectional RNN
Huawei equipment configures channel switching services without interruption
Applet framework taro
DOM//
Intelligent metal detector based on openharmony
POJ 3176-Cow Bowling(DP||记忆化搜索)
Detailed explanation of MATLAB cov function
跨境电商是啥意思?主要是做什么的?业务模式有哪些?
sklearn模型整理
Operators
Function///
[there may be no default font]warning: imagettfbbox() [function.imagettfbbox]: invalid font filename
Process control
R3Live系列学习(四)R2Live源码阅读(2)
Cron表达式(七子表达式)
不要再说微服务可以解决一切问题了!
Operation of simulated examination platform of special operation certificate examination question bank for safety production management personnel of hazardous chemical production units in 2022
Codeforces Round #804 (Div. 2)
【广告系统】Parameter Server分布式训练