using System.Xml
XmlNode : IEnumerable , ICloneable , IXPathNavigable
XmlElement
XmlDocument
XmlAttribute
XmlNodeList : IEnumerable , IDisposable
1.增
XmlDocument doc = new XmlDocument();
//添加说明
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
//添加根节点
XmlElement root = doc.CreateElement("Books");
doc.AppendChild(root);
//添加二级
XmlElement bookIn = doc.CreateElement("In");
XmlElement bookOut = doc.CreateElement("Out");
root.AppendChild(bookIn);
root.AppendChild(bookOut);
//添加三级(带属性)
XmlElement 苍井空 = doc.CreateElement("book");
苍井空.SetAttribute("Name", "苍老师");
苍井空.SetAttribute("Category", "写真");
苍井空.SetAttribute("Msg", "有利于身心健康");
bookIn.AppendChild(苍井空);
//保存 Path不存在会自动创建
doc.Save(Path);
<Books>
<IN>
<book Msg="有利于身心健康" Category="写真" Name="苍老师">
</IN>
<OUT>
</OUT>
</Books>
2.删
XmlDocument doc = new XmlDocument();
//加载 FileNotFoundException-->文件不存在
doc.Load(Path);
//获取根标签
XmlNode root = doc.DocumentElement;
//删除子节点
root.RemoveChild(root.FirstChild);
//删除属性
XmlElement ele = (XmlElement)(root.FirstChild.FirstChild);
ele.RemoveAttribute("Name");
doc.Save(Path);
3.改
...
...
//修改属性
first.SetAttribute("Name","?????");
//修改文本
first.InnerText = "?????";
doc.Save(BookCfg);
4.查
...
...
//SelectSingleNode <SystemCfg>根节点--><IN>子元素(跳过根节点找<IN>结果为NULL)
XmlNode IN=doc.SelectSingleNode("SystemCfg/IN");
//SelectNodes
XmlNodeList INS = doc.SelectNodes("SystemCfg/IN/IN");
XmlNode ele = INS.Item(0) | INS[0];
5.属性|方法
Name | MSG |
---|
Load(fileName) | Save(fileName) | 加载 | 保存 |
DocumentElement | 获取根节点 |
SelectSingleNode(xpath) | XmlNode符合表达式的第一个 |
SelectNodes(xpath) | XmlNodeList符合表达式的全部 |
ChildNodes | XmlNodeList下一级的所有子节点 |
FirstChild | LastChild | 下一级的第一个子元素 |
ParentNode | 上一级父元素 |
NextSibling | PreviousSibling | 同级下一个 |
CreateXmlDeclaration(“1.0”, “UTF-8”, “yes”) | 创建文档说明 |
CreateElement(name) | 创建节点 |
AppendChild(newChild) | PrependChild(newChild) | 追加 |
SetAttribute(name,value) | 设置|添加属性 |
GetAttribute(name) | 获取属性 |
InnerText | 获取|设置文本 |
HasAttribute | True有属性 |
HasChildNodes | True有子节点 |
RemoveChild(name) | 删除该节点 |
RemoveAttribute(name) | 删除该属性 |
System.Xml.XDocument
using System.Xml.Linq
abstract XObject : IXmlLineInfo
abstract XNode
abstract XContainer
XDocment
XElement
XAttribute
1.增
XDocument doc = new XDocument();//说明自动创建
//添加根节点
XElement root = new XElement("Books");
doc.Add(root);
//添加二级节点
XElement 苍井空 = new XElement("苍井空");
苍井空.SetAttributeValue("nickNmae", "苍老师");
苍井空.SetElementValue("Category","写真");
苍井空.SetElementValue("Msg","有利于身心健康");
root.Add(苍井空);
//保存
doc.Save(BookCfg);
<Books>
<苍井空 nickNmae="苍老师">
<Category>写真</Category>
<Msg>有利于身心健康</Msg>
</苍井空>
</Books>
2.查
XDocument doc = XDocument.Load(url);//加载文档
XElement root = doc.Root;//获取根路径
//获取根下的所有子节点
IEnumerable<XElement> chileNode = root.Elements();
XElement IN = chileNode.ElementAt(0);
XElement OUT = chileNode.ElementAt(1);
//<IN>--><IN Index="8" WizeNo="EXI200" CardNo="0" Name="启动按钮"/>
XElement node=(XElement)IN.FirstNode;//方式一
XElement node=IN.Elements().FirstOrDefault(node => node.Attribute("Name").Value == "启动按钮");//方式二
String Name=node.Attribute("Name").Value;