1、LINQ to XML类
以下的代码演示了怎样运用LINQ to XML来疾速建立一个xml:
隐蔽行号 复制代码 ? 建立 XML
public static void CreateDocument() { string path = @"d:\website"; XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "root")); xdoc.Save(path); }
运转该示例将会取得一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <Root>root</Root>
2、XElement类
XElement 类是 LINQ to XML 中的基本类之一。 它示意一个 XML 元素。 可以运用该类建立元素;变动元素内容;增加、变动或删除子元素;向元素中增加属性;或以文本格式序列化元素内容。 还可以与 System.Xml 中的其他类(比方 XmlReader、XmlWriter 和 XslCompiledTransform)举行互支配。
运用LINQ to XML建立xml文档有很多种体式格局,细致运用哪一种要领要根据实际须要。而建立xml文档最简朴、最常见的体式格局是运用XElement类。以下的代码演示了怎样运用XElement类建立一个xml文档:
显现行号 复制代码 ? 这是一段程序代码。
public static void CreateCategories() { string path = @"d:\website"; XElement root = new XElement("Categories", new XElement("Category", new XElement("CategoryID", Guid.NewGuid()), new XElement("CategoryName", "Beverages") ), new XElement("Category", new XElement("CategoryID", Guid.NewGuid()), new XElement("CategoryName", "Condiments") ), new XElement("Category", new XElement("CategoryID", Guid.NewGuid()), new XElement("CategoryName", "Confections") ) ); root.Save(path); }
运转该示例将会取得一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category> <CategoryID>57485174-46fc-4e8c-8d98-d25b53d504a1</CategoryID> <CategoryName>Beverages</CategoryName> </Category> <Category> <CategoryID>1474dde1-8014-48f7-b093-b47ca5d5b770</CategoryID> <CategoryName>Condiments</CategoryName> </Category> <Category> <CategoryID>364224e0-e002-4939-90fc-0fd93e0cf35b</CategoryID> <CategoryName>Confections</CategoryName> </Category> </Categories>
XElement类包括了很多要领,这些要领使得处置惩罚xml变得易如反掌。有关这些要领请参照MSDN。
个中,Save、CreateReader、ToString和WriteTo要领是比较经常使用的三个要领:
3、XAttribute类
XAttribute类用来处置惩罚元素的属性,属性是与元素相干联的“称号-值”对,每一个元素中不能有称号反复的属性。运用XAttribute类与运用XElement类的支配十分相似,下面的示例演示了怎样在建立xml树时为其增加一个属性:
显现行号 复制代码 ? 这是一段程序代码。
public static XElement CreateCategoriesByXAttribute() { XElement root = new XElement("Categories", new XElement("Category", new XAttribute("CategoryID", Guid.NewGuid()), new XElement("CategoryName", "Beverages") ), new XElement("Category", new XAttribute("CategoryID", Guid.NewGuid()), new XElement("CategoryName", "Condiments") ), new XElement("Category", new XAttribute("CategoryID", Guid.NewGuid()), new XElement("CategoryName", "Confections") ) ); root.Save(path); return root; }
运转该示例将会取得一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category CategoryID="a6d5ef04-3f83-4e00-aeaf-52444add7570"> <CategoryName>Beverages</CategoryName> </Category> <Category CategoryID="67a168d5-6b22-4d82-9bd4-67bec88c2ccb"> <CategoryName>Condiments</CategoryName> </Category> <Category CategoryID="17398f4e-5ef1-48da-8a72-1c54371b8e76"> <CategoryName>Confections</CategoryName> </Category> </Categories>
XAttribute类的要领比较少,经常使用的三个是:
以下的示例运用Remove来删除第一个元素的CategoryID属性:
显现行号 复制代码 ? 这是一段程序代码。
public static void RemoveAttribute() { XElement xdoc = CreateCategoriesByXAttribute(); XAttribute xattr = xdoc.Element("Category").Attribute("CategoryID"); xattr.Remove(); xdoc.Save(path); }
运转该示例将会取得一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category> <CategoryName>Beverages</CategoryName> </Category> <Category CategoryID="5c311c1e-ede5-41e5-93f7-5d8b1d7a0346"> <CategoryName>Condiments</CategoryName> </Category> <Category CategoryID="bfde8db5-df84-4415-b297-cd04d8db9712"> <CategoryName>Confections</CategoryName> </Category> </Categories>
作为尝试,试一试以下删除属性的要领:
public static void RemoveAttributeByDoc() { XElement xdoc = CreateCategoriesByXAttribute(); XAttribute xattr = xdoc.Attribute("CategoryID"); xattr.Remove(); xdoc.Save(path); }
运转该示例将会抛出一个空援用非常,由于元素Categories没有一个叫做CategoryID的属性。
4、XDocument类
XDocument类供应了处置惩罚xml文档的要领,包括声明、解释和处置惩罚指令。一个XDocument对象可以包括以下内容:
下面的示例建立了一个简朴的xml文档,它包括几个元素和一个属性,以及一个处置惩罚指令和一些解释:
public static void CreateXDocument() { XDocument xdoc = new XDocument( new XProcessingInstruction("xml-stylesheet", "title='EmpInfo'"), new XComment("some comments"), new XElement("Root", new XElement("Employees", new XElement("Employee", new XAttribute("id", "1"), new XElement("Name", "Scott Klein"), new XElement("Title", "Geek"), new XElement("HireDate", "02/05/2007"), new XElement("Gender", "M") ) ) ), new XComment("more comments") ); xdoc.Save(path); }
运转该示例将会取得一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet title='EmpInfo'?> <!--some comments--> <Root> <Employees> <Employee id="1"> <Name>Scott Klein</Name> <Title>Geek</Title> <HireDate>02/05/2007</HireDate> <Gender>M</Gender> </Employee> </Employees> </Root> <!--more comments-->
XDocument类包括多个与XElement类雷同的要领,细致内容可以参阅MSDN。须要注重的是,处置惩罚节点和元素的大部分功用都可以经由历程XElement取得,只有当相对须要文档条理的处置惩罚才能,以及须要接见解释、处置惩罚指令和声明时,才有运用XDocument类的必要。
建立了xml文档后,可以运用NodesAfterSelf要领返回指定的XElement元素以后的一切同级元素。须要注重的是,此要领只包括返回鸠合中的同级元素,而不包括子代。此要领运用耽误实行。以下代码演示了这一历程:
显现行号 复制代码 ? 这是一段程序代码。
public static void NodesAfterSelf() { XElement root = new XElement("Categories", new XElement("Category", new XElement("CategoryID", Guid.NewGuid()), new XElement("CategoryName", "食物"), new XElement("Description", "可以吃的东西") ) ); foreach (var item in root.Element("Category").Element("CategoryID").NodesAfterSelf()) { Console.WriteLine((item as XElement).Value); } }
二、LINQ to XML编程观点
本节将引见LINQ to XML编程的相干观点,比方怎样加载xml、建立全新xml、支配xml的信息以及遍历xml文档。
1、加载已有的xml
运用LINQ to XML加载xml可以从多种数据源取得,比方字符串、XmlReader、TextReader或文件。
下面的示例演示了怎样从文件中加载xml:
public static void LoadFromFile() { XElement root = XElement.Load(path); Console.WriteLi
也可以运用Parse要领从一个字符串加载xml:
public static void LoadFromString() { XElement root = XElement.Parse(@" <Categories> <Category> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Category> </Categories> "); Console.WriteLine(root.ToString()); }
2、保留xml
在前面的示例中曾屡次挪用XElement对象的Save要领来保留xml文档,在这里就不冗述了。
3、建立xml
在前面的示例中曾屡次挪用XElement对象的组织函数来建立xml文档,在这里就不冗述了。须要申明的是,在运用LINQ to XML建立xml文档时,会有代码缩进,这使代码的可读性大大增强。
4、遍历xml
运用LINQ to XML在xml树中遍历xml是相称简朴的。只须要运用XElement和XAttribute类中所供应的要领。Elements和Element要领供应了定位到某个或某些元素的体式格局。下面的示例演示了怎样遍历xml树,并猎取指定元素的体式格局:
public static void Enum() { XElement root = new XElement("Categories"); using (NorthwindDataContext db = new NorthwindDataContext()) { root.Add( db.Categories .Select ( c => new XElement ( "Category" , new XElement("CategoryName", c.CategoryName) ) ) ); } foreach (var item in root.Elements("Category")) { Console.WriteLine(item.Element("CategoryName").Value); } }
上述代码运转的效果为:
是否是很简朴呢?Nodes()、Elements()、Element(name)和Elements(name)要领为xml树的导航供应了基本功用。
5、支配xml
LINQ to XML一个主要的特征是可以方便地修正xml树,如增加、删除、更新和复制xml文档的内容。
I.插进去
运用XNode类的插进去要领可以方便地向xml树增加内容:
鄙人面的示例中,运用AddAfterSelf要领向现有xml中增加一个新节点:
public static void AddAfterSelf() { XElement root = XElement.Parse(@" <Categories> <Category> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Category> </Categories> "); XElement xele = root.Element("Category").Element("CategoryName"); xele.AddAfterSelf(new XElement("AddDate", DateTime.Now)); root.Save(path); }
运转该示例将会取得一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> <AddDate>2010-01-31T03:08:51.813736+08:00</AddDate> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Category> </Categories>
当须要增加一个元素到指定节点之前时,可以运用AddBeforeSelf要领。
II.更新
在LINQ to XML中更新xml内容可以运用以下几种要领:
鄙人面的示例中运用了ReplaceWith与SetElementValue要领对xml举行了更新支配:
public static void Update() { XElement root = XElement.Parse(@" <Categories> <Category> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Category> </Categories> "); root.Element("Category").Element("CategoryID").ReplaceWith(new XElement("ID", "2")); root.Element("Category").SetElementValue("CategoryName", "test data"); root.Save(path); }
运转该示例将会取得一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category> <ID>2</ID> <CategoryName>test data</CategoryName> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Category> </Categories>
III.删除
可以运用Remove(XElement)与RemoveAll要领来删除xml。
鄙人面的示例中,运用了RemoveAll要领:
} public static void Remove() { string path = @"d:\"; XElement root = XElement.Parse(@" <Categories> <Category> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Category> </Categories> "); root.RemoveAll(); root.Save(path); }
运转该示例将会取得一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories />
鄙人面的示例中,运用了Remove要领删除了xml的Description元素:
public static void Remove() { XElement root = XElement.Parse(@" <Categories> <Category> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Category> </Categories> "); root.Element("Category").Element("Description").Remove(); root.Save(path); }
运转该示例将会取得一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> </Category> </Categories>
6、处置惩罚属性
I.增加
LINQ to XML增加属性与增加元素师相似的,可以运用组织函数或许Add要领来增加属性:
public static void AddAttribute() { XElement root = new XElement("Categories", new XElement("Category", new XAttribute("CategoryID", "1"), new XElement("CategoryName", "Beverages"), new XElement("Description", "Soft drinks, coffees, teas, beers, and ales") ) ); root.Element("Category").Add(new XAttribute("AddDate", DateTime.Now.ToShortDateString())); root.Save(path); }
运转该示例将会取得一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category CategoryID="1" AddDate="2010-01-31"> <CategoryName>Beverages</CategoryName> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Category> </Categories>
II.检索
检索属性可以运用Attribute(name)要领:
显现行号 复制代码 ? 这是一段程序代码。
public static void SelectAttribute() { XElement root = new XElement("Categories", new XElement("Category", new XAttribute("CategoryID", "1"), new XElement("CategoryName", "Beverages"), new XElement("Description", "Soft drinks, coffees, teas, beers, and ales") ) ); XAttribute xattr = root.Element("Category").Attribute("CategoryID"); Console.WriteLine(xattr.Name); Console.WriteLine(xattr.Value); }
上述代码的运转效果为:
CategoryID 1
本文总结
本文引见了LINQ to XML的编程基本,即System.Xml.Linq定名空间中的多个LINQ to XML类,这些类都是LINQ to XML的支撑类,它们使得处置惩罚xml比运用其他的xml东西轻易很多。在本文中,偏重引见的是XElement、XAttribute和XDocument。
以上就是LINQ to XML 编程基本的图文代码细致引见的内容,更多相干内容请关注ki4网(www.ki4.cn)!