1. 讀取XML檔案
XDocument和XElement類都提供了匯入XML檔案的Load()方法,可以讀取XML檔案的內容,並轉換為XDocument或XElement類的例項。
示例XML檔案:Product.xml
<?xml version="1.0" encoding="utf-8"?> <Products> <Product> <ProductID>1</ProductID> <ProductName>LINQ to XML</ProductName> <UnitPrice>10</UnitPrice> </Product> <Product> <ProductID>2</ProductID> <ProductName>LINQ to SQL</ProductName> <UnitPrice>20</UnitPrice> </Product> </Products>
XElement root = XElement.Load(Server.MapPath("Product.xml"));
2. 查詢根元素
XDocument doc = XDocument.Load(Server.MapPath("Product.xml")); IEnumerable<XElement> elements = from e in doc.Elements("Products") select e; foreach (XElement e in elements) { Console.WriteLine("{0}-{1}", e.Name, e.Value); }
XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\Product.xml"); IEnumerable<XElement> elements = from e in doc.Elements("Products") select e; foreach (XElement e in elements) { Console.WriteLine("{0}-{1}", e.Name, e.Value); }
3. 查詢節點
XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\" + "Product.xml"); var query = from p in doc.Element("Products").Elements("Product") where (int)p.Element("ProductID") == 1 select p; query.ToList().ForEach(item => { Console.WriteLine("{0}-{1}-{2}", item.Element("ProductID").Value, item.Element("ProductName").Value, item.Element("UnitPrice").Value); });
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml"); var query = from p in xml.Elements("Product") where (int)p.Element("ProductID") == 1 select p;
4. 查詢子孫節點
Descendants軸方法與Elements型別,不過Elements只查詢當前元素下的直接子節點,而Descendants則會遍歷當前元素下任意層級的子元素。
示例xml檔案:
<?xml version="1.0" encoding="utf-8"?> <Products> <Product> <Book> <ProductID>1</ProductID> <ProductName>LINQ to XML</ProductName> <UnitPrice>10</UnitPrice> </Book> </Product> <Product> <Book> <ProductID>2</ProductID> <ProductName>LINQ to SQL</ProductName> <UnitPrice>20</UnitPrice> </Book> </Product> </Products>
使用Descendants()方法查詢:
XElement root = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml"); var query = from b in root.Descendants("Book") select b; foreach (var item in query) { Console.WriteLine(item.Element("ProductName").Value); }
5. 查詢屬性
示例xml檔案:
<?xml version="1.0" encoding="utf-8"?> <Products> <Product ID="1"> <ProductID>1</ProductID> <ProductName>LINQ to XML</ProductName> <UnitPrice>10</UnitPrice> </Product> <Product ID="2"> <ProductID>2</ProductID> <ProductName>LINQ to SQL</ProductName> <UnitPrice>20</UnitPrice> </Product> </Products>
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml"); var query = from p in xml.Elements("Product") where (int)p.Attribute("ID") == 1 select p;
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml"); var query = from p in xml.Elements("Product") where (int)p.Attribute("ID") == 1 select new { ID = p.Attribute("ID").Value, ProductID = p.Element("ProductID").Value, ProductName = p.Element("ProductName").Value };
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml"); var query = from p in xml.Nodes().OfType<XElement>() where (int)p.Attribute("ID") == 1 select new { ID = p.Attribute("ID").Value, ProductID = p.Element("ProductID").Value, ProductName = p.Element("ProductName").Value };