LINQ To XML詳細描述
在向大家詳細介紹LINQ To XML——XML操作之前,首先讓大家了解下LINQ to XML框架是一個輕量級的XML編程API,然后全面介紹XML操作。
LINQ To XML——XML操作
XML數(shù)據(jù)越來越廣泛地應(yīng)用在各種實際的開發(fā)系統(tǒng)中,為了簡化對XML數(shù)據(jù)的開發(fā)和利用,微軟的開發(fā)團隊開發(fā)了這個全新的LINQ to XML框架。首先,LINQ to XML框架是一個輕量級的XML編程API,開發(fā)者利用該框架,幾乎可以取代原有的XML數(shù)據(jù)開發(fā)方式,非常簡單地創(chuàng)建、讀取并操作內(nèi)存中的XML數(shù)據(jù),如利用函數(shù)構(gòu)造方法創(chuàng)建XML樹等。其次,LINQ to XML框架中集成了LINQ的強大功能,開發(fā)者可以采用一致的編程方式,非常容易地操作并查詢XML數(shù)據(jù)。
- XElement Students = new XElement("Students",
 - new XElement("Student",
 - new XElement("Name", "張三"),
 - new XElement("Sex", "男"),
 - new XElement("Age", 20)),
 - new XElement("Student",
 - new XElement("Name", "李四"),
 - new XElement("Sex", "女"),
 - new XElement("Age", 19))
 - );
 - Console.WriteLine(Students);
 - 編歷XML
 - XElement Students = new XElement("Students",
 - new XElement("Student",
 - new XElement("Name", "張三"),
 - new XElement("Sex", "男"),
 - new XElement("Age", 20)),
 - new XElement("Student",
 - new XElement("Name", "李四"),
 - new XElement("Sex", "女"),
 - new XElement("Age", 19))
 - );
 - foreach (XNode node in Students.Nodes())
 - {
 - Console.WriteLine(node);
 - Console.WriteLine("----------------------------");
 - }
 - foreach (XElement ele in Students.Elements())
 - {
 - Console.WriteLine(ele);
 - Console.WriteLine("********************************");
 - }
 
添加XML節(jié)點
- XElement Students = new XElement("Students",
 - new XElement("Student",
 - new XElement("Name", "張三"),
 - new XElement("Sex", "男"),
 - new XElement("Age", 20)),
 - new XElement("Student",
 - new XElement("Name", "李四"),
 - new XElement("Sex", "女"),
 - new XElement("Age", 19))
 - );
 - foreach (XElement ele in Students.Elements())
 - {
 - ele.Element("Age").AddAfterSelf(new XElement("Hight", 173));
 - ele.Element("Age").AddBeforeSelf(new XElement("Weight", 73));
 - ele.Add (new XElement("Hobby", "Coding"));
 - }
 - Console.WriteLine(Students)
 - 更新XML節(jié)點
 - XElement Students = new XElement("Students",
 - new XElement("Student",
 - new XElement("Name", "張三"),
 - new XElement("Sex", "男"),
 - new XElement("Age",new XAttribute ("Year",1989/8/22), 20))
 - );
 - Students.Element(“Student”).Element(“Age”).ReplaceWith(new XElement(“Age”, 28));
 - //替換掉整個節(jié)點
 - // Students.Element(“Student”).Element(“Age”).ReplaceNodes ( 28);//只替換節(jié)點值
 - // Students.Element(“Student”).Element(“Age”).ReplaceAll (28);//替換掉整個節(jié)點
 - Console.WriteLine(Students);
 - 刪除XML節(jié)點
 - XElement Students = new XElement("Students",
 - new XElement("Student",
 - new XElement("Name", "張三"),
 - new XElement("Sex", "男"),
 - new XElement("Age",new XAttribute ("Year","1989/8/22"), 20))
 - );
 - //Students.Element("Student").Element("Age").Remove ();//移除節(jié)點
 - //Students.Element("Student").Element("Age").RemoveAll();//移除節(jié)點的值和屬性
 - Students.Element("Student").Element("Age").RemoveNodes();//移除節(jié)點的值
 - Console.WriteLine(Students);
 - 添加XML屬性
 - XElement Students = new XElement("Students",
 - new XElement("Student",
 - new XElement("Name", "張三"),
 - new XElement("Sex", "男"),
 - new XElement("Age",new XAttribute ("Year","1989/8/22"), 20))
 - );
 - Students.Element("Student").SetAttributeValue("dd","dddd");
 - Console.WriteLine(Students);
 - 更新XML屬性
 - Students.Element("Student").Element("Age").ReplaceAttributes(new XAttribute("Year","dd"));
 - Students.Element("Student").Element("Age").SetAttributeValue("Year", "dddd");
 - 刪除XML屬性
 - Students.Element("Student").Element("Age").Attribute("Year").Remove ();
 - Students.Element("Student").Element("Age").RemoveAttributes ();
 - 遍歷XML屬性
 - var Attr = from att in Students.Element("Student").Element("Age").Attributes()
 - select att;
 - foreach (var att in Attr)
 - {
 - Console.WriteLine(att);
 - }
 
【編輯推薦】















 
 
 
 
 
 
 