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();
}
}
}