假如有以下XML:
<?xml version="1.0" encoding="utf-8" ?>
<path>D:888</path>
<path>韩寒_要自由.htm</path>
<category>民主</category>
</file>
<path>谈谈韩寒三论.htm</path>
<category>民主中国</category>
</file>
<path>韩寒_我的2011.htm</path>
<category>民主中国</category>
</file>
<path>韩寒_要自由.htm</path>
<category>民主中国</category>
</file>
<path>韩寒_说民主.htm</path>
<category>民主中国</category>
</file>
<path>韩寒_谈革命.htm</path>
<category>民主中国</category>
</file>
</files>
</files>
</packages>
</export>
可采用以下方式进行读取
class Program { public static XElement InitializeXMLDate() { string XMLPath = @"D:附件1 - 副本.xml"; if(!File.Exists(XMLPath)) { throw new FileNotFoundException("The XML data file is missing"); } XElement data = XElement.Parse(File.ReadAllText(XMLPath)); return data; } static void Main(string[] args) { var packages = InitializeXMLDate().Elements().Where(element => element.Name.ToString() == "packages"); foreach (var package in packages.Elements()) { Console.WriteLine(package.Attribute("id").Value); Console.WriteLine(package.Element("path").Value); // var files = package.Elements().Where(element => element.Name.ToString()=="file"); foreach (var file in package.Elements().Where(element => element.Name.ToString()=="files")) { foreach (var property in file.Elements().Where(element => element.Name.ToString() == "file")) { Console.WriteLine(string.Format("{0}--------{1}", property.Element("path").Value, property.Element("category").Value)); } Console.WriteLine("****"); } Console.WriteLine("-----------------------------------"); } Console.ReadKey(); } }