LINQ to XML
读写XML有很多技术,Dom、Sax等,还有高级的读写技术XmlSerializer、 Linq To XML(System.Xml.Linq)、 System.Xml(XMLDocument)等, XmlSerializer要求对每种不同的文件都定义一套类,很麻烦,而Linq To XML则不需要单独创建类,当然更底层一些,代码比XmlSerializer多,灵活性更高。System.Xml下的类是2.0及之前操作xml推荐的,现在很多人也仍然在用这个namespace下的类,这个namespace下的类和Linq To XML非常相似,因此不用单独学。
核心类XElement,一个XElement表示一个节点,new XElement("Order");创建一个名字为Order的标签,调用Add增加子节点,也是XElement 对象,和TreeView一样。
想得到字符串怎么办?ToString
调用XElement的Save方法将xml内容保存在Writer中
1.xElement# |
表示一个 XML 元素。
命名空间: System.Xml.Linq
程序集: System.Xml.Linq(在 System.Xml.Linq.dll 中)
static void Main(string[] args)
{
XElement xPerson = new XElement("Persons");
XElement xMan = new XElement("Person");
XElement xWoman = new XElement("Person");
xMan.Value = "Jerry";
xMan.SetAttributeValue("hand",2); //设置节点属性
//错误的写法:xMan.Attribute("age").Value = "30";
xPerson.Add(xMan);
xPerson.Add(xWoman);
string xml = xPerson.ToString();
Console.WriteLine(xml);
Console.ReadKey();
}
2.XDocument# |
表示 XML 文档。
命名空间: System.Xml.Linq
程序集:System.Xml.Linq(在 System.Xml.Linq.dll 中)
XDocument doc = XDocument.Load(reader),加载XML文件,XDocument就是加载的对象
using (Stream stream = File.OpenRead(@"../../app.xml"))
{
using (StreamReader reader = new StreamReader(stream))
{
XDocument xdoc = XDocument.Load(reader);
//xdoc.Root //XDocument的Root属性表示根节点
//读取根节点的v1属性:111
string v1 = xdoc.Root.Attribute("v1").Value;//读取根节点的属性
Console.WriteLine(v1);
//读取子节点的数量:2
Console.WriteLine(xdoc.Root.Nodes().Count());
//读取第1个子节点:<Person age="25">xMan</Person>
XNode node = xdoc.Root.Nodes().ElementAt(0);
Console.WriteLine(node);
//读取xMan的属性age:25
XElement xe1 = (XElement)node;
string xMan = xe1.Value;//元素
string age = xe1.Attribute("age").Value;//属性
Console.WriteLine("{0}的属性age为:{1}.", xMan, age);
Console.ReadKey();
}
}
3.读取节点<connectionStrings>的add的属性# |
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!--fffff-->
<connectionStrings>
<add name="c1" connectionString="DateSource=....."/>
<add name="c2" connectionString="DateSource=127.0.0.1"/>
</connectionStrings>
<!--aaaaa-->
</configuration>
using (Stream stream = File.OpenRead(@"../../app.config"))
{
using (StreamReader reader = new StreamReader(stream))
{
XDocument xdoc = XDocument.Load(reader);
IEnumerable<XElement> connectrs =
xdoc.Root.Elements("connectionStrings");
if (connectrs.Count()==1)
{
XElement xCon = connectrs.ElementAt(0);
foreach (XElement item in xCon.Elements("add"))
{
string name = item.Attribute("name").Value;
string connstr = item.Attribute("connectionString").Value;
Console.WriteLine("名字:{0},字符串:{1}", name, connstr);
}
}
}
}
Console.ReadKey();
4.练习:读取属性NavigationPage的值# |
<?xml version="1.0" encoding="utf-8" ?>
<Persons v1="111">
<add name="Jerry">
<Tasks>
<Name>NRG</Name>
<DefaulTask NavigationPage="DemoPage1.xmal">HOT</DefaulTask>
</Tasks>
</add>
</Persons>
代码如下:
using (Stream stream = File.OpenRead(@"../../WMappManifest.xml"))
{
using (StreamReader reader =new StreamReader(stream))
{
XDocument xdoc = XDocument.Load(reader);
var data = xdoc.Root.Element("add").Element("Tasks").Element("DefaulTask");
Console.WriteLine(data.Attribute("NavigationPage").Value);
}
}
Descendants()的用法:
有且只有1个节点,结果如上图
using (Stream stream = File.OpenRead(@"../../WMappManifest.xml"))
{
using (StreamReader reader =new StreamReader(stream))
{
XDocument xdoc = XDocument.Load(reader);
//var data = xdoc.Root.Element("add").Element("Tasks").Element("DefaulTask");
//Console.WriteLine(data.Attribute("NavigationPage").Value);
//Descendants()有且只有唯一子节点,
//慎用,容易取错点
var Adata=xdoc.Descendants("DefaulTask").Single();
Console.WriteLine(Adata.Attribute("NavigationPage").Value);
}
}
作者:【唐】三三
出处:https://www.cnblogs.com/tangge/archive/2012/07/17/2595011.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具