1 XML file contents
1<?xml version="1.0"encoding="Utf-8"?>2<cultures>3 4<daojia count=" -"Comment="Good">5<book>6<name> Ethics through </name>7<author> Lao Zi </author>8</book>9<book >Ten<name> the beginning of the Canon </name> One<author> Guan Yin </author> A</book> -</daojia> - the<fojia count="321"Comment="Good"> -<book > -<name> Diamond Sutra </name> -<author> Sakyamuni Buddha </author> +</book> -<book> +<name> Tibetan Bodhisattva Sutra </name> A<author> Buddha in Tibet </author> at</book> -</fojia> - -</cultures>
2 Code
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Xml.Linq;7 8 namespaceConsoleApplication89 {Ten class Program One { A Static voidMain (string[] args) - { - //1.xml file must be in the correct format theXDocument file = Xdocument.load ("1.xml"); - - //gets the root node of the XML file -XElement XmlRoot =file. Root; + - //gets the first level of child nodes under the root node +Ienumerable<xelement> nodefirst=xmlroot.elements (); A at foreach(varIteminchNodefirst) - { - - variattres=item. Attributes (); - foreach(varIinchiattres) - { inConsole.WriteLine (i.name+" "+i.value); - } to } + - Console.readkey (); the } * } $}
3 effects
C # XML output the name and value of all properties of the direct (first-level) child node under the XML root node