When loading XML using the load method, you will find the difference between the two:
The following is a brief summary:
Xdocument. Load () load the entire XML file including the root node
Xelement. Load () does not load the XML Root Node
Xelement. Load () Sample Code:
File.WriteAllText("Test.xml", @"<Root> <Child1>1</Child1> <Child2>2</Child2> <Child3>3</Child3></Root>");Console.WriteLine("Querying tree loaded with XElement.Load");Console.WriteLine("----");XElement doc = XElement.Load("Test.xml");IEnumerable<XElement> childList = from el in doc.Elements() select el;foreach (XElement e in childList) Console.WriteLine(e);
Result:
Querying tree loaded with XElement.Load----<Child1>1</Child1><Child2>2</Child2><Child3>3</Child3>
Xdocument. Load () sample code:
File.WriteAllText("Test.xml", @"<Root> <Child1>1</Child1> <Child2>2</Child2> <Child3>3</Child3></Root>");Console.WriteLine("Querying tree loaded with XDocument.Load");Console.WriteLine("----");XDocument doc = XDocument.Load("Test.xml");IEnumerable<XElement> childList = from el in doc.Elements() select el;foreach (XElement e in childList) Console.WriteLine(e);
Result:
Querying tree loaded with XDocument.Load----<Root> <Child1>1</Child1> <Child2>2</Child2> <Child3>3</Child3></Root>
For more information, see msdn: http://msdn.microsoft.com/zh-cn/library/bb675196 (V = vs.90). aspx