using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Web;
using System.Xml.Linq; namespace XMLOperation
{
class Program
{
static void Main(string[] args)
{
/*=============Linq 读写XML==================*/
string wxmlPath = @"F:\XmlTest\test.xml";
XmlWriteReadLinqOperation writeReadLinq = new XmlWriteReadLinqOperation();
// writeReadLinq.WriteXml(wxmlPath);
writeReadLinq.CreatXmlTree(wxmlPath); //string xmlPath = @"F:\XML.xml";
string xmlPath = @"C:\Users\zery.zhang\Desktop\ProjectDemo\XML.xml";
/*
* 1 三者之间的关系用图画出
* 2 XMLElement 主要是针对节点的一些属性进行操作
* 3 XMLDocument 主要是针对节点的CUID操作
* 4 XMLNode 为抽象类,做为以上两类的基类,提供一些操作节点的方法
*/ //===========C# to Xml==========//
XmlOperation xmlOperation = new XmlOperation();
//xmlOperation.Create(xmlPath);
//xmlOperation.CreateAttribute(xmlPath); //xmlOperation.Delete(xmlPath);
//xmlOperation.DeleteAttribute(xmlPath); //xmlOperation.Modify(xmlPath);
//xmlOperation.ModifyAttribute(xmlPath); //xmlOperation.Select(xmlPath);
//xmlOperation.SelectAttribute(xmlPath);
/*=============Linq to Xml===========*/
XmlOperationToLinq xOperation = new XmlOperationToLinq();
// xOperation.Create(xmlPath);
/*
*1 给指定的XML节点的所有子节点增加一个节点,并增加属性
*2 删除指定节点的子节点的指定属性
*3
*/
string lxmlPath = @"F:\XmlTest\test.xml";
xOperation.Create(lxmlPath);
xOperation.CreateAttribute(lxmlPath);
//xperation.Delete(lxmlPath);
//xOperation.DeleteAttribute(lxmlPath);
// xOperation.ModifyAttribute(lxmlPath); /*=============C# 读写XML===============*/
XmlWriteReadOperation writeRead = new XmlWriteReadOperation(); Console.Read(); } } class XmlOperation
{ public void Create(string xmlPath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
var root = xmlDoc.DocumentElement;//取到根结点 XmlNode newNode = xmlDoc.CreateNode("element", "Name", "");
newNode.InnerText = "Zery"; //添加为根元素的第一层子结点
root.AppendChild(newNode);
xmlDoc.Save(xmlPath);
}
//属性
public void CreateAttribute(string xmlPath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("Collection/Book");
node.SetAttribute("Name", "C#");
xmlDoc.Save(xmlPath);
} public void Delete(string xmlPath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
var root = xmlDoc.DocumentElement;//取到根结点 var element = xmlDoc.SelectSingleNode("Collection/Name");
root.RemoveChild(element);
xmlDoc.Save(xmlPath);
} public void DeleteAttribute(string xmlPath)
{ XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("Collection/Book");
//移除指定属性
node.RemoveAttribute("Name");
//移除当前节点所有属性,不包括默认属性
node.RemoveAllAttributes(); xmlDoc.Save(xmlPath); } public void Modify(string xmlPath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
var root = xmlDoc.DocumentElement;//取到根结点
XmlNodeList nodeList = xmlDoc.SelectNodes("/Collection/Book");
//xml不能直接更改结点名称,只能复制然后替换,再删除原来的结点
foreach (XmlNode node in nodeList)
{
var xmlNode = (XmlElement)node;
xmlNode.SetAttribute("ISBN", "Zery");
}
xmlDoc.Save(xmlPath); } public void ModifyAttribute(string xmlPath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("Collection/Book");
element.SetAttribute("Name", "Zhang");
xmlDoc.Save(xmlPath); } public void Select(string xmlPath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
//取根结点
var root = xmlDoc.DocumentElement;//取到根结点
//取指定的单个结点
XmlNode singleNode = xmlDoc.SelectSingleNode("Collection/Book"); //取指定的结点的集合
XmlNodeList nodes = xmlDoc.SelectNodes("Collection/Book"); //取到所有的xml结点
XmlNodeList nodelist = xmlDoc.GetElementsByTagName("*");
} public void SelectAttribute(string xmlPath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("Collection/Book");
string name = element.GetAttribute("Name"); }
} class XmlOperationToLinq
{
//其它操作
public void OtherOperaton()
{
//加文件头
} public void Create(string xmlPath)
{
XDocument xDoc = XDocument.Load(xmlPath);
XElement xElement = xDoc.Element("BookStore");
xElement.Add(new XElement("Test", new XAttribute("Name", "Zery")));
xDoc.Save(xmlPath);
} public void CreateAttribute(string xmlPath)
{
XDocument xDoc = XDocument.Load(xmlPath);
IEnumerable<XElement> xElement = xDoc.Element("BookStore").Elements("Book");
foreach (var element in xElement)
{
element.SetAttributeValue("Name", "Zery");
}
xDoc.Save(xmlPath);
} public void Delete(string xmlPath)
{
XDocument xDoc = XDocument.Load(xmlPath);
XElement element = (XElement)xDoc.Element("BookStore").Element("Book");
element.Remove();
xDoc.Save(xmlPath);
} public void DeleteAttribute(string xmlPath)
{
XDocument xDoc = XDocument.Load(xmlPath);
//不能跨级取节点
XElement element = xDoc.Element("BookStore").Element("Book").Element("Name");
element.Attribute("BookName").Remove();
xDoc.Save(xmlPath);
} public void ModifyAttribute(string xmlPath)
{
XDocument xDoc = XDocument.Load(xmlPath);
XElement element = xDoc.Element("BookStore").Element("Book");
element.SetAttributeValue("BookName","ZeryTest");
xDoc.Save(xmlPath);
} } internal class XmlWriteReadOperation
{ } class XmlWriteReadLinqOperation
{ public void CreatXmlTree(string xmlPath)
{
XElement xElement = new XElement(
new XElement("BookStore",
new XElement("Book",
new XElement("Name", "C#入门", new XAttribute("BookName", "C#")),
new XElement("Author", "Martin", new XAttribute("Name", "Martin")),
new XElement("Adress", "上海"),
new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
),
new XElement("Book",
new XElement("Name", "WCF入门", new XAttribute("BookName", "WCF")),
new XElement("Author", "Mary", new XAttribute("Name", "Mary")),
new XElement("Adress", "北京"),
new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
)
)
); //需要指定编码格式,否则在读取时会抛:根级别上的数据无效。 第 1 行 位置 1异常
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new UTF8Encoding(false);
settings.Indent = true;
XmlWriter xw = XmlWriter.Create(xmlPath,settings);
xElement.Save(xw);
//写入文件
xw.Flush();
xw.Close();
} public void WriteXml(string xmlPath)
{
XElement xElement = new XElement(
new XElement("Store",
new XElement("Book", "技术类",
new XElement("Name", "C#入门", new XAttribute("BookName", "C#")),
new XElement("Author", "Martin", new XAttribute("Name", "Zery")),
new XComment("以下为注释"),//xml注释
new XElement("Date", DateTime.Now.ToString(), new XAttribute("PublicDate", DateTime.Now.ToString()))
))
); XmlWriter xw = XmlWriter.Create(xmlPath);
//保存到XmlWriter
xElement.Save(xw);
//写入文件
xw.Flush();
xw.Close(); } }
}

<转载>XML操作的更多相关文章

  1. 【转】python XML 操作总结(创建、保存和删除,支持utf-8和gb2312)

    原文地址:http://hi.baidu.com/tbjmnvbagkfgike/item/6743ab10af43bb24f6625cc5 最近写程序需要用到xml操作,看了看python.org上 ...

  2. ASP.NET控件GridView的使用& Xml操作注意事项

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢!   原文链接:http://www.cnblogs.com/zishi/p/6729478.html 文章主要内容 ...

  3. c# xml操作(二)

    c# xml操作(二) 此博文包含图片 (-- ::)转载▼ 标签: 杂谈 分类: c# 上次,我们介绍了增加和删除xml的一些操作,这次我们将介绍如何更改和读取xml特定节点.我们依然以上次的xml ...

  4. c# xml操作(一)

    c# xml操作(一) 此博文包含图片 (-- ::)转载▼ 标签: 杂谈 分类: c# 同样是增,删,改,查四种操作.现在我们就详细介绍一下增和删两种操作.看下面的一个xml示例: <?xml ...

  5. Open XML操作Excel导入数据

    项目中发现使用OleDb(using System.Data.OleDb)相关对象处理Excel导入功能,不是很稳定经常出问题,需要把这个问题解决掉.项目组提出使用OpenXML来处理Excel的导入 ...

  6. LINQ系列:LINQ to XML操作

    LINQ to XML操作XML文件的方法,如创建XML文件.添加新的元素到XML文件中.修改XML文件中的元素.删除XML文件中的元素等. 1. 创建XML文件 string xmlFilePath ...

  7. T-Sql(五)xml操作

    t-sql中的xml操作在我们平时做项目的过程中用的很少,因为我们处理的数据量很少,除非一些用到xml的地方,t-sql中xml操作一般用在数据量很大,性能优化的地方,当然我在平时做项目的时候也是没用 ...

  8. XML格式示例 与 XML操作(读取)类封装

    header('Content-Type: text/xml'); <?xml version="1.0" encoding="utf-8" standa ...

  9. 【Java EE 学习 33 上】【JQuery样式操作】【JQuery中的Ajax操作】【JQuery中的XML操作】

    一.JQuery中样式的操作 1.给id=mover的div采用属性增加样式.one $("#b1").click(function(){ $("#mover" ...

随机推荐

  1. C# 堆栈(Stack)和队列(Queue)

    一.什么是堆?(Heap)      堆是无序的,是一片不连续的内存域,由用户自己来控制和释放,如果用户自己不释放的话,当内存达到一定的特定值时,通过垃圾回收器(GC)来回收.      是程序运行期 ...

  2. XVII Open Cup named after E.V. Pankratiev. GP of Moscow Workshops

    A. Centroid Tree 枚举至多两个重心作为根,检查对于每个点是否都满足$2size[x]\leq size[father[x]]$即可. #include<stdio.h> # ...

  3. [POJ1723]SOLDIERS(中位数)

    题意 给出n个点的坐标,它们只能往上.下.左.右一格一格地移动,求使其移动至水平线上的最小步数. 思路 转载 先易后难,对于纵向的问题,我们推个公式,,这个很容易看出是货仓选址问题,k取y[i]的中位 ...

  4. 做rl_abs过程中遇到的问题

    问题一 运行 train_abstractor.py就出现这个问题 nohup: ignoring input start training with the following hyper-para ...

  5. Codechef August Challenge 2018 : Safe Partition

    传送门 (虽然是A了但是不知道复杂度是不是正确的 考虑以某个位置为结尾的合法划分 先考虑min,带来的影响是限制了最小长度,预处理出这个最小长度后,这可以在处理到这个数时,把不能算的部分去掉(不满足m ...

  6. 在Linux下开发多语言软件(gettext解决方案)

    最近的项目出现了一个bug.项目是基于一个已有的成熟开源软件之上做修改的,新写了加解密库,用于为该成熟开源软件增添加解密功能.功能增加完成后效果都很好,可是就是中文出不来了,也就是说没办法自适应多语言 ...

  7. eclipse创建spring boot项目,tomcat启动成功,但http://localhost:8080无法访问报错404解决方案

    spring boot的启动程序启动后,在访问http://localhost:8080地址的时候出现了错误,为什么出错网上我找了好久也没有得出具体的解决办法 当我指定到具体的action的时候,却可 ...

  8. c++第七周学习小结

    时间过得好快,一下就上了好多节c++的课了.我们现在新学了控制语句和函数,这对我来说有点难.老师上课的时候ppt过得有些快,自己会跟不上,所以以后上课前要预习充分.在经过同学和搜索引擎帮助后,我完成了 ...

  9. wpf1

    emCombobox.Items[2].IsEnabled = false; 隐藏下拉框里面的一个item wpf 单例模式. [DllImport("user32", CharS ...

  10. stm32通用定时器详解

    在stm32的开发中我们经常会用到定时器,因此在学习stm32的过程中定时器是必须要学的,而定时主要又分为三大类分别为: 高级控制定时器(TIM1与TIM8) 通用定时器(TIM2~TIM5) 基本定 ...