1.xml

<?xml version="1.0" encoding="utf-8" ?>
<stories>
<story ac="">
<title>A House in Aungier Street</title>
<author>
<name>Sheridan le Fanu</name>
<nationality>Irish</nationality>
</author>
<rating>eerie</rating>
</story>
<story ac ="">
<title>The Signalman</title>
<author>
<name>Charles Dickens</name>
<nationality>English</nationality>
</author>
<rating>atmospheric</rating>
</story> </stories>

代码,添加一个按钮,并绑定到btnLoop_Click方法,添加lable txtResult即可

private void btnLoop_Click(object sender, EventArgs e)
{ XmlDocument doc = new XmlDocument();
string filePath =@"D:\文档\VS2010项目\XMLTest\XMLTest\XMLFile1.xml";
doc.Load(filePath); //添加节点
XmlElement newBook = doc.CreateElement("story");
XmlElement newTitle = doc.CreateElement("Title");
XmlElement newAuthor = doc.CreateElement("author");
XmlElement newName = doc.CreateElement("name");
XmlElement newNationality = doc.CreateElement("nationality");
XmlElement newRating =doc.CreateElement("rating"); XmlText title = doc.CreateTextNode("Beginning VS2010");
XmlText name = doc.CreateTextNode("Karing");
XmlText nationality = doc.CreateTextNode("UK");
XmlText rating = doc.CreateTextNode("4 star");
XmlComment comment = doc.CreateComment("The Previous Version"); newBook.AppendChild(comment);
newBook.AppendChild(newTitle);
newBook.AppendChild(newAuthor);
newAuthor.AppendChild(newName);
newAuthor.AppendChild(newNationality);
newBook.AppendChild(newRating); newTitle.AppendChild(title);
newName.AppendChild(name);
newNationality.AppendChild(nationality);
newRating.AppendChild(rating); XmlNode root = doc.DocumentElement;
root.InsertAfter(newBook, root.FirstChild);//放到第一个子节点之后 doc.Save("xmlFileAddNode.xml"); //删除节点
root.RemoveChild(root.FirstChild);//删除第一本书
doc.Save("deleteNode.xml"); //在不遍历xml文档的情况下选择节点
txtResult.Text = FormatText(root, "", "");//遍历xml
} //通过循环xml节点把xml写入字符串
private string FormatText(XmlNode node, string text, string indent)
{
if (node is XmlText)
{
text += node.Value;
return text;
} if (string.IsNullOrEmpty(indent))
indent = "";
else
text += "\r\n" + indent; if (node is XmlComment)//注释节点
{
text += node.OuterXml;//OuterXml:当前节点的XML,InnerXml:当前节点开始标签和结束标签之间的XML,Value 只有XmlText,XmlAttribute,XmlComment有值,文本节点的文本值或者属性值
return text;//注InnerText返回当前节点所有字节的文本,如<book><title>111</title><author>222</author></book>,book节点的InnerText是111222
} text += "<" + node.Name;
if (node.Attributes.Count > )
{
AddAttributes(node,ref text);//属性
} if (node.HasChildNodes)
{
text += ">";
foreach (XmlNode child in node.ChildNodes)
{
text = FormatText(child, text, indent + " ");
} if(node.ChildNodes.Count ==&&(node.FirstChild is XmlText || node.FirstChild is XmlComment))
text +="</"+node.Name +">";
else
text +="\r\n"+"</"+node.Name +">";
}else
text +=" />"; return text;
} private void AddAttributes(XmlNode node ,ref string text)
{
foreach (XmlAttribute xa in node.Attributes)
{
text += " " + xa.Name + "='" + xa.Value + "'";
}
}

2.遍历XML即添加修改节点的更多相关文章

  1. 用dom4j修改xml(增加修改节点)

    用dom4j修改xml(增加修改节点) 博客分类: Java XMLJavaMyeclipseServlet  使用dom4j修改解析xml,xml文件的位置是配置在xml.properties文件中 ...

  2. Idea开发环境中,开发springboot类型的项目,如果只引入parent节点,不添加依赖节点,maven是不会加载springboot的任何依赖的

    在SpringBoot类型的项目中,我本来是要使用pringBoot,创建一个Console项目,我原本在pom.xml中添加paren节点了,天真的认为不需要再添加其他任何依赖了,可是接下来的1个小 ...

  3. C# xml 读xml、写xml、Xpath、Xml to Linq、xml添加节点 xml修改节点

    #region XDocument //创建XDocument XDocument xdoc2 = new XDocument(); XElement xel1= new XElement(" ...

  4. Dom4j向XML中指定位置添加、删除、修改节点——(五)

    需求: 在第一本书作者后面增加描述 <描述>好书</描述>  思路:获取书下面下的所有节点(一个list集合),在list集合指定位置添加一个元素(list.add(index ...

  5. C#中操作xml文件(插入节点、修改、删除)

    已知有一个xml文件(bookstore.xml)如下: <?xml version="1.0" encoding="gb2312"?> <b ...

  6. 使用DOM解析XML文件,、读取xml文件、保存xml、增加节点、修改节点属性、删除节点

    使用的xml文件 <?xml version="1.0" encoding="GB2312" ?> <PhoneInfo> <Br ...

  7. c#操作xml的代码(插入节点、修改节点、删除节点等)

    bookstore.xml文件内容: 复制代码代码示例: <?xml version="1.0" encoding="gb2312"?><bo ...

  8. 用TinyXml做XML解析示例 TinyXml查找唯一节点及修改节点操作

    // 读者对象:对TinyXml有一定了解的人.本文是对TinyXml工具的一些知识点的理解. // 1 TinyXml中对TiXmlNode进行了分类,是用一个枚举进行描述的. // enum No ...

  9. 问题:jQuery中遍历XML文件时候,获取子节点children不支持的情况(已解决)

    问题描述: 今天在写一个基于 jquery 的读取xml文件的程序时候,需要遍历xml的节点. 代码片段如下: function parse_xml_node(parent,result){ // r ...

随机推荐

  1. app内嵌h5页面在ios手机端滑动卡顿的解决方法

    1.带滚动条的dom需加样式 -webkit-overflow-scrolling: touch;2.去掉 width:100%; height:100%

  2. mac自带ssh在哪

    打开Finder,前往> 前往文件夹,输入 ~/.ssh 回车就到ssh目录了. 点击Mac顶部菜单-->“苹果”图标-->最近使用的项目-->终端--输入 open ~/.s ...

  3. 在ensp上配置Hybrid接口

    Hybrid接口是华为特有的一种接口 Hybrid接口是既可以连接普通终端的接入链路,又可以连接交换机间的干道链路. 简单说就是Hybrid接口既能实现Access的功能又能实现Trunk接口的功能. ...

  4. 酷!微软发布新终端工具,Win 10 将自带 Linux 内核

    原创:技术最前线(id:TopITNews) 北京时间 5 月 7 日,2019 年微软 Build 开发者大会在雷德蒙德召开.今年大会上亮点很多,本文汇总一些和开发者相关的内容. 1. Window ...

  5. python 之 Django框架(APP和ORM的使用)

    12.3 APP 12.31 创建APP 一个Django项目可以分为很多个APP,用来隔离不同功能模块的代码 用命令行创建一个APP: python3 manage.py startapp app0 ...

  6. Github-Q&A 常见错误排查(持续更新)

    Q1:提交Git时出现"remote origin already exists"提示?   关联本地与网上仓库的时候,可能会出现此错误,这是因为已经存在已关联的仓库.通过如下方法 ...

  7. MAT实战:JVM内存溢出的定位与分析

  8. Spring AOP创建AroundAdvice实例

    AroundAdvice 1.在方法之前和之后来执行相应的操作 2.实现MethodInterceptor接口 接口文件: public interface IHello { public void ...

  9. Socket HttpListen

    HttpListener sSocket = new HttpListener(); sSocket.Prefixes.Add("http://127.0.0.1:8080/"); ...

  10. power shell命令添加SharePoint用户组与用户(用户为域用户)

    查看SharePoint用户组 Get-PnPGroup 查看某一用户组 Get-PnPGroup -Identity "用户组名" 查看某一用户组下的所有成员 Get-PnPGr ...