前端之家收集整理的这篇文章主要介绍了
Linq对XML的操作,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace LinQToXML
{
class Program
{
static void Main(string[] args)
{
string xml =Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"CompanyXml.xml");
//***********************************LinQ创建XML****************************************
//XElement xe = new XElement("Company",// new XElement("CompanyName","特维科技"),// new XElement("CompanyAddress","科技园"),// new XElement("CompanyTel","1808833456"),// new XElement("CompanyBos",// new XElement("Name","马化疼"),// new XElement("Age","46"),// new XElement("Sex","男")),// new XElement("Country","ShenZhen"));
//Console.WriteLine(xe.ToString());
//XDocument xd = new XDocument(xe);
//xd.Save(xml);
//***************************XNamespace对象表示XML名称空间*************************************
//XNamespace ns = "http://www.baidu.com/ns/1";
//XElement xe = new XElement(ns+"Company",// new XElement("CompanyName",// new XElement("CompanyAddress",// new XElement("CompanyTel",// new XElement("CompanyBos",// new XElement("Name",// new XElement("Age",// new XElement("Sex",// new XElement("Country","ShenZhen"));
//Console.WriteLine(xe.ToString());
//**************************XComment可以在XML文档中任意添加注释********************************
//XDocument xdoc=new XDocument();
//xdoc.Add(new XComment("这是一个开头注释:开始创建XML文档"));
//XElement xe = new XElement("Company",// new XComment("公司的地址是:科技园"),"ShenZhen"));
//Console.WriteLine(xe.ToString());
//xdoc.Add(xe);
//xdoc.Save(xml);
//*********************************XAttribute对象用来添加和使用特性******************************
//XDocument xdoc = new XDocument();
//XElement xe = new XElement("Company",// new XAttribute("特别属性","中国500强"),// new XAttribute("公司地址面积","不详"),// "科技园"),"李开放"),"ShenZhen"));
//Console.WriteLine(xe.ToString());
//xdoc.Add(xe);
//xdoc.Save(xml);
//************************************LinQ对XML的查询*****************************************
if (File.Exists(xml))
{
XDocument xdoc = XDocument.Load(xml);
var info = from c in xdoc.Descendants("CompanyName")
select c.Value;
//等同于==
var info1 = xdoc.Descendants("CompanyName").Select(x => x.Value);
Console.WriteLine("Name的个数:{0}",info.Count());
foreach (var item in info)
{
Console.WriteLine(item);
}
var info2 = from c in xdoc.Descendants("CompanyBos")
select new
{
Name = from n in c.Elements("Name")
select new { name = n.Value },Age = c.Element("Age").Value,Sex = c.Element("Sex").Value
};
Console.WriteLine("CompanyBos的子集数:{0}",info2.Count());
foreach (var item in info2)
{
foreach (var n in item.Name)
{
Console.WriteLine("Name:{0}",n.name);
}
Console.WriteLine(string.Format("Age:{0}\nSex:{1}",item.Age,item.Sex));
}
//等同于==
IEnumerator<XElement> xles = xdoc.Element("Company").Elements("CompanyBos").GetEnumerator();
while (xles.MoveNext())
{
XElement xe = xles.Current;
int count = xe.Elements("Name").Count();
IEnumerator<XElement> names = xe.Elements("Name").GetEnumerator();
while (names.MoveNext())
{
XElement nameXe = names.Current;
string name = nameXe.Value;
Console.WriteLine("Name:{0}",name);
}
string age = xe.Element("Age").Value;
string sex = xe.Element("Sex").Value;
Console.WriteLine(string.Format("Age:{0}\nSex:{1}",age,sex));
}
Console.WriteLine(xdoc);
//string value = xdoc.Element("CompanyName").Value;//这种是错误的写法,不能越级查询某节点的值
string value = xdoc.Element("Company").Element("CompanyName").Value;
Console.WriteLine(value);
}
//****************************LinQ对XML的修改、增加、删除************************************
//if (File.Exists(xml))
//{
// XDocument xdoc = XDocument.Load(xml);
//Console.WriteLine(xdoc.Element("Company").Element("CompanyName").Value);
//Console.WriteLine("Company修改为:");
//xdoc.Element("Company").Element("CompanyName").SetValue("HuaWei");
//xdoc.Element("Company").Element("CompanyName").ReplaceAll("LanXiang");
//Console.WriteLine(xdoc.Element("Company").Element("CompanyName").Value);
//Console.WriteLine(xdoc);
//xdoc.Element("Company").AddFirst(new XElement("CreatYear","1988年"));//在Company首行增加CreatYear
//xdoc.Element("Company").Add(new XElement("CreatYear","1988年"));//在Company尾行增加CreatYear
//xdoc.Element("Company").Element("CompanyName").RemoveAll();//移除节点和属性
//xdoc.Element("Company").Element("CompanyName").Remove();//此处删除此节点
//xdoc.Element("Company").Element("CompanyName").RemoveAttributes();//移除属性
//XAttribute abb=xdoc.Element("Company").Attribute("特别属性");//修改属性
////abb.SetValue("经济强国");
//abb.Value = "经济强国";
//}
Console.ReadKey();
}
}
}