70-487 WCF (Windows Communication Foundation) Web Services
In the book Exam Ref 70-487 page 99 exemple Root.Descendants vs Root.Elements
using System; using System.Linq; using System.Xml.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTest { [TestClass] public class XmlTest { [TestMethod] public void DescVsElemReturnsSameCounts() { string documentData = @" <Customers> <Customer> <FirstName>Bill</FirstName> </Customer> <Customer> <FirstName>Joe</FirstName> </Customer> </Customers>"; XDocument docSample = XDocument.Parse(documentData); var descendantsQuery =
from desc in docSample.Root.Descendants("Customer")
select desc; Int32 descendantsCount = descendantsQuery.Count(); Console.WriteLine("desc :" + descendantsCount);
// desc : 2
var elementsQuery =
from elem in docSample.Root.Elements("Customer") select elem;
Int32 elementsCount = elementsQuery.Count(); Console.WriteLine("elem :" + elementsCount);
// elem : 2} [TestMethod] public void DescVsElemWithNestedElementReturnSameCount() { string documentData = @" <Root> <CustomerName> <FirstName>John</FirstName> </CustomerName> <CustomerName> <FirstName>Bill</FirstName> </CustomerName> <CustomerName> <Other> <Another> Blah </Another> </Other> <FirstName>Joe</FirstName> <MiddleInitial>Q</MiddleInitial> <LastName>Joe</LastName> </CustomerName> </Root>"; XDocument docSample = XDocument.Parse(documentData); var descendantsQuery =
from desc in docSample.Root.Descendants("CustomerName")
select desc; Int32 descendantsCount = descendantsQuery.Count(); Console.WriteLine("desc :" + descendantsCount);
// desc : 3var elementsQuery =
from elem in docSample.Root.Elements("CustomerName") select elem;
Int32 elementsCount = elementsQuery.Count(); Console.WriteLine("elem :" + elementsCount);
// elem : 3 } [TestMethod] public void DescVsElemWithSameNestedElementReturnDifferents
Count() { string documentData = @" <Root> <CustomerName> <FirstName>John</FirstName> </CustomerName> <CustomerName> <FirstName>Bill</FirstName> </CustomerName> <CustomerName> <Other> <CustomerName> Blah </CustomerName> </Other> <FirstName>Joe</FirstName> <MiddleInitial>Q</MiddleInitial> <LastName>Joe</LastName> </CustomerName> </Root>"; XDocument docSample = XDocument.Parse(documentData); var descendantsQuery =
from desc in docSample.Root.Descendants("CustomerName")
select desc; Int32 descendantsCount = descendantsQuery.Count(); Console.WriteLine("desc :" + descendantsCount);
// desc : 4var elementsQuery =
from elem in docSample.Root.Elements("CustomerName") select elem;
Int32 elementsCount = elementsQuery.Count(); Console.WriteLine("elem :" + elementsCount);
// desc : 3} } }
elem doesn't investigates the child elements