首页 > 学院 > 开发设计 > 正文

C#读写xml

2019-11-08 02:14:17
字体:
来源:转载
供稿:网友

读:

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的子节点branchXmlNode 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);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表