XMLElement,XMLDocument 用法「建议收藏」

XMLElement,XMLDocument 用法「建议收藏」来源http://www.cr173.com/html/23515_1.html一前言先来了解下操作XML所涉及到的几个类及之间的关系 如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家*1XMLElement主要是针对节点的一些属性进行操作*2XMLDocument主要是针对节点的CUID操作*3XMLNode为抽象

大家好,又见面了,我是你们的朋友全栈君。

来源http://www.cr173.com/html/23515_1.html

一 前言

先来了解下操作XML所涉及到的几个类及之间的关系  如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家

* 1 XMLElement 主要是针对节点的一些属性进行操作
* 2 XMLDocument 主要是针对节点的CUID操作
* 3 XMLNode 为抽象类,做为以上两类的基类,提供一些操作节点的方法

XMLElement,XMLDocument 用法「建议收藏」

清楚了以上的关系在操作XML时会更清晰一点

二 具体操作

以下会对Xml的结点与属性做增 删 改 查的操作也满足了实际工作中的大部分情况 

先构造一棵XML树如下,其中也涉及到了写入xml文档的操作

 1         public void CreatXmlTree(string xmlPath) 2         { 3             XElement xElement = new XElement( 4                 new XElement("BookStore", 5                     new XElement("Book", 6                         new XElement("Name", "C#入门", new XAttribute("BookName", "C#")), 7                         new XElement("Author", "Martin", new XAttribute("Name", "Martin")), 8                         new XElement("Adress", "上海"), 9                         new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))10                         ),11                     new XElement("Book",12                         new XElement("Name", "WCF入门", new XAttribute("BookName", "WCF")),13                         new XElement("Author", "Mary", new XAttribute("Name", "Mary")),14                         new XElement("Adress", "北京"),15                         new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))16                         )17                         )18                 );19 20             //需要指定编码格式,否则在读取时会抛:根级别上的数据无效。 第 1 行 位置 1异常21             XmlWriterSettings settings = new XmlWriterSettings();22             settings.Encoding = new UTF8Encoding(false);23             settings.Indent = true;24             XmlWriter xw = XmlWriter.Create(xmlPath,settings);25             xElement.Save(xw);26             //写入文件27             xw.Flush();28             xw.Close();29         }

然后得到如下的XML树

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <BookStore>
 3     <Book>
 4         <Name BookName="C#">C#入门</Name>
 5         <Author Name="Martin">Martin</Author>
 6         <Date>2013-10-11</Date>
 7         <Adress>上海</Adress>
 8         <Date>2013-10-11</Date>
 9     </Book>
10     <Book>
11         <Name BookName="WCF">WCF入门</Name>
12         <Author Name="Mary">Mary</Author>
13         <Adress>北京</Adress>
14         <Date>2013-10-11</Date>
15     </Book>
16 </BookStore>

以下操作都是对生成的XML树进行操作

2.1 新增节点与属性

新增节点NewBook并增加属性Name=”WPF”

1         public void Create(string xmlPath) 2         { 3             XmlDocument xmlDoc = new XmlDocument(); 4             xmlDoc.Load(xmlPath); 5              6             var root = xmlDoc.DocumentElement;//取到根结点 7             XmlNode newNode = xmlDoc.CreateNode("element", "NewBook", ""); 8             newNode.InnerText = "WPF"; 9 10             //添加为根元素的第一层子结点11             root.AppendChild(newNode);12             xmlDoc.Save(xmlPath);13         }

开篇有写操作xml节点属性主要用XmlElement对象所以取到结点后要转类型 

 1         //属性
 2         public void CreateAttribute(string xmlPath)
 3         {
 4             XmlDocument xmlDoc = new XmlDocument();
 5             xmlDoc.Load(xmlPath);
 6             var root = xmlDoc.DocumentElement;//取到根结点
 7             XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
 8             node.SetAttribute("Name", "WPF");
 9             xmlDoc.Save(xmlPath);
10 
11         }

效果如下

XMLElement,XMLDocument 用法「建议收藏」

2.2 删除节点与属性

 1         public void Delete(string xmlPath)
 2         {
 3             XmlDocument xmlDoc = new XmlDocument();
 4             xmlDoc.Load(xmlPath);
 5             var root = xmlDoc.DocumentElement;//取到根结点
 6 
 7             var element = xmlDoc.SelectSingleNode("BookStore/NewBook");
 8             root.RemoveChild(element);
 9             xmlDoc.Save(xmlPath);
10         }

删除属性

 1         public void DeleteAttribute(string xmlPath)
 2         {
 3             XmlDocument xmlDoc = new XmlDocument();
 4             xmlDoc.Load(xmlPath);
 5             XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
 6             //移除指定属性
 7             node.RemoveAttribute("Name");
 8             //移除当前节点所有属性,不包括默认属性
 9             //node.RemoveAllAttributes();
10             xmlDoc.Save(xmlPath);
11         }

2.3 修改节点与属性

xml的节点默认是不允许修改的,本文也就不做处理了

  修改属性代码如下

1         public void ModifyAttribute(string xmlPath)
2         {
3             XmlDocument xmlDoc = new XmlDocument();
4             xmlDoc.Load(xmlPath);
5             XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
6             element.SetAttribute("Name", "Zhang");
7             xmlDoc.Save(xmlPath);
8         }

效果如下

XMLElement,XMLDocument 用法「建议收藏」

2.4 获取节点与属性

 1         public void Select(string xmlPath)
 2         {
 3             XmlDocument xmlDoc = new XmlDocument();
 4             xmlDoc.Load(xmlPath);
 5             //取根结点
 6             var root = xmlDoc.DocumentElement;//取到根结点
 7             //取指定的单个结点
 8             XmlNode oldChild = xmlDoc.SelectSingleNode("BookStore/NewBook");
 9             
10             //取指定的结点的集合
11             XmlNodeList nodes = xmlDoc.SelectNodes("BookStore/NewBook");
12 
13             //取到所有的xml结点
14             XmlNodeList nodelist = xmlDoc.GetElementsByTagName("*");
15         }

属性

1         public void SelectAttribute(string xmlPath)
2         {
3             XmlDocument xmlDoc = new XmlDocument();
4             xmlDoc.Load(xmlPath);
5             XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
6             string name = element.GetAttribute("Name");
7             Console.WriteLine(name);
8         }

三  linq to XML 操作

 Linq to Xml 也没什么变化只操作对象改变了主要涉及的几个对象如下   注:我并没有用linq的语法去操作元素。

XDocument:用于创建一个XML实例文档

XElement:用于一些节点与节点属性的基本操作

以下是对Xml的 一些简单的操作

3.1 新增节点与属性

1         public void Create(string xmlPath)
2         {
3             XDocument xDoc = XDocument.Load(xmlPath);
4             XElement xElement = xDoc.Element("BookStore");
5             xElement.Add(new XElement("Test", new XAttribute("Name", "Zery")));
6             xDoc.Save(xmlPath);
7         }

属性

 1         public void CreateAttribute(string xmlPath)
 2         {
 3             XDocument xDoc = XDocument.Load(xmlPath);
 4             IEnumerable<XElement> xElement = xDoc.Element("BookStore").Elements("Book");
 5             foreach (var element in xElement)
 6             {
 7                 element.SetAttributeValue("Name", "Zery");
 8             }
 9             xDoc.Save(xmlPath);
10         }

3.2 删除节点与属性

1         public void Delete(string xmlPath)
2         {
3             XDocument xDoc = XDocument.Load(xmlPath);
4             XElement element = (XElement)xDoc.Element("BookStore").Element("Book");
5             element.Remove();
6             xDoc.Save(xmlPath);
7         }

属性

1         public void DeleteAttribute(string xmlPath)
2         {
3             XDocument xDoc = XDocument.Load(xmlPath);
4             //不能跨级取节点
5             XElement element = xDoc.Element("BookStore").Element("Book").Element("Name");
6             element.Attribute("BookName").Remove();
7             xDoc.Save(xmlPath);
8         }

3.3 修改节点属性

节点.net没提供修改的方法本文也不做处理

修改属性与新增实质是同一个方法

1         public void ModifyAttribute(string xmlPath)
2         {
3             XDocument xDoc = XDocument.Load(xmlPath);
4             XElement element = xDoc.Element("BookStore").Element("Book");
5             element.SetAttributeValue("BookName","ZeryTest");
6             xDoc.Save(xmlPath);
7         }

四 总结

把文章写完时,又扫去了自己的一个盲区,虽然都是些简单的操作,但在实际的开中,又何尝不是由简单到复杂呢。我觉得身为程序员就应该遇到自己的盲区时,立马花时间去了解,不说要了解多深入,但至少基本的还是要知道,等到工作中真需时,只要稍微花点时间就可以了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150754.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java工厂模式_java工厂模式

    java工厂模式_java工厂模式java工厂模式分三种:简单工厂模式、工厂方法模式、抽象工厂模式。简单工厂模式(SimpleFactoryPattern)属于类的创新型模式,又叫静态工厂方法模式(StaticFactoryMethodPattern),是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。简单工厂模式就是通过一个”全能类”,根据外界传递的信息来决定创建哪个具体类的对象。如下图(懒得…

    2022年7月9日
    30
  • php7使用curl扩展「建议收藏」

    php7使用curl扩展「建议收藏」  前言:最近项目中要调用一些接口,看到网上很多都使用curl,但由于刚开始,php很多的语法都不是很熟悉,例如如何调用第三方函数等,为了使用curl_init()等函数,从安装php的扩展curl开始踩了很多坑,对于环境安装真的是比较头疼的事情,往往可能因为一些小问题而不成功,而且按照网上乱七八糟的博客说的做,真的一点用都没有,特此记录一下,希望以后的编程生涯中尽量少犯这种错误。首先给出环境…

    2022年10月21日
    3
  • drupal 6.0 入门教程 – 第一章

    drupal 6.0 入门教程 – 第一章
     
    由于工作项目的原因,需要采用drupal来部署,所以最近学习了drupalcms,天天到 drupal.org,drupalchina.org,zhupou.cn,5iphp.com上学习
     
     
    项目的核心是提供一款在线教学和互动社区,希望通过这个教程提供给大家一个比较全面的项目开发指导。首先,我近期的主要任务是熟悉drupalCMS,和设计主页的版式也就是themes。
     
    下面我们从drupal的介绍入手,开始讲解如果

    2022年5月29日
    36
  • 如何更改layui弹出层样式「建议收藏」

    如何更改layui弹出层样式「建议收藏」div设置屏幕宽度

    2022年5月10日
    43
  • B4j教程_ubuntu以太坊挖矿

    B4j教程_ubuntu以太坊挖矿Bminer产品介绍Bminer是目前最快的挖矿程序,Bminer是基于NVIDIAGPU深度优化的挖矿软件。Bminer支持Equihash和Ethash两种算法的虚拟币,包括:ETH(以太坊),ETC,ZEC(零币),ZCL,ZEN,HUSH,BitcoinPrivate,KMD,BitcoinGold(比特币黄金)等币种。挖Ethash的币,比如ETH(以太坊),ETC,Bminer仅…

    2022年10月15日
    3
  • 【转载】如何实施异构服务器的负载均衡及过载保护?

    【转载】如何实施异构服务器的负载均衡及过载保护?

    2021年11月20日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号