Today, for project reasons, we need to use C # to parse the XML file. Because we use the 3.5 framework, we want to try the water with the LINQ to XML. So I tried it on msdn.
Code
Xelement Root = Xelement. Load ( " Projectsoruce. xml " );
Ienumerable< Xelement > Resources =
From El In Root. descendants ( " Task " )
Select El;
Foreach (Xelement el In Resources)
Console. writeline (EL );
The task element cannot be obtained. It's a bit confusing, and there are just a few examples on msdn. After debugging for an hour, I finally found that my XML has a namespace, And the namespace needs to be included during the query of the LINQ to XML.CodeIt should be written like this
@ "Http://schemas.microsoft.com/project"; is the namespace on my own XML
Code
Xnamespace NS = @" Http://schemas.microsoft.com/project " ;
Xelement Root=Xelement. Load ("Projectsoruce. xml");
Ienumerable < Xelement > Resources =
From El In Root. element (NS + " Tasks " ). Elements (NS + " Task " )
Select El;
Foreach (Xelement el In Resources)
Console. writeline (EL );
// Pause the application
Console. Readline ();