使用 Linq-to-XML:
XDocument document = XDocument.Load("MyDocument.xml"); // Loads the XML document with to use with Linq-to-XML
var booms = from boomElement in document.Descendants("boom") // Go through the collection of boom elements
select String.Format("name: {0}" + Environment.NewLine + "address: {1}", // Format the boom item
boomElement.Element("name").Value, // Gets the name value of the boom element
boomElement.Element("address").Value); // Gets the address value of the boom element
var result = String.Join(Environment.NewLine + Environment.NewLine, booms); // Concatenates all boom items into one string with
更新
用 中的任何元素来概括它boom
,这个想法是一样的。
var booms = from boomElement in document.Descendants("boom") // Go through the collection of boom elements
let boolChildren = (from boomElementChild in boomElement.Elements() // Go through the collection of elements in the boom element
select String.Format("{0}: {1}", // Formats the name of the element and its value
boomElementChild.Name.LocalName, // Name of the element
boomElementChild.Value)) // Value of the element
select String.Join(Environment.NewLine, boolChildren); // Concatenate the formated child elements
第一行和最后一行保持不变。