C#读写xml

读:

XmlDocument xmlDoc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;
XmlReader reader = null;
reader = XmlReader.Create(fileName, settings);
xmlDoc.Load(reader);
//获取名为Root的节点
XmlNode root = xmlDoc.SelectSingleNode("Root");
//节点的属性,clazz属性值
XmlAttribute xmlAttr = root.Attributes["clazz"];
String type = xmlAttr == null ? null : xmlAttr.Value;
//遍历这个节点的子节点
foreach(XmlNode subNode in root){
    //节点值
    subNode.Value;
}

写:

XmlDeclaration Declaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.InsertBefore(Declaration, xmlDoc.DocumentElement);
//创建Root节点
XmlNode rootNode = xmlDoc.CreateElement("Root");
xmlDoc.AppendChild(rootNode);
//Root的子节点branch
XmlNode branch=xmlDoc.CreateElement("branch");
rootNode.AppendChild(branch);
//给branch添加属性type="branch"
XmlAttribute attr=xmlDoc.CreateAttribute("type");
attr.Value="branch";
branch.Attributes.Append(attr);
//保存到文件
xmlDoc.save(fileName);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值