c#xml基本使用

1 篇文章 0 订阅

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.属性|方法

NameMSG
Load(fileName) | Save(fileName)加载 | 保存
DocumentElement获取根节点
SelectSingleNode(xpath)XmlNode符合表达式的第一个
SelectNodes(xpath)XmlNodeList符合表达式的全部
ChildNodesXmlNodeList下一级的所有子节点
FirstChild | LastChild下一级的第一个子元素
ParentNode上一级父元素
NextSibling | PreviousSibling同级下一个
CreateXmlDeclaration(“1.0”, “UTF-8”, “yes”)创建文档说明
CreateElement(name)创建节点
AppendChild(newChild) | PrependChild(newChild)追加
SetAttribute(name,value)设置|添加属性
GetAttribute(name)获取属性
InnerText获取|设置文本
HasAttributeTrue有属性
HasChildNodesTrue有子节点
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;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值