c#操作xml文件_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > c#操作xml文件

c#操作xml文件

 2014/12/24 19:14:33  GC2013  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml;namespacedelegatedemo{publicclassXmlClass{privatestringxmlPath;publicXmlClass(stringxmlPath){this
  • 标签:C# 文件 操作 XML文件 XML

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);
}
}
}

发表评论
用户名: 匿名