LINQ to XML Get all node names from file

belaj_mala

New Member
I got a XML file like this.\[code\]Parent-Value-Value-Child--Value--Value---Grandchild----Value---GrandchildEND-ChildEND-Value-Child2......\[/code\]Structure of the XML file is variable. Means each XML has different Childs , Grandchilds, etc.I want to open the XML file, and input the data from the XML file to a MYSQL database.Here the Code i use to open the XML and input the information to MySQL.\[code\] while (hardcore < 50000) { try { XDocument xmlDoc2 = XDocument.Load("c:\\a.xml"); var simpleQuery2 = (from Mainparent in xmlDoc2.Descendants("Mainparent") select Mainparent).FirstOrDefault(); dbcon.Insert ( simpleQuery2.Element("id").Value, simpleQuery2.Element("action").Value, simpleQuery2.Element("ordernr").Value, simpleQuery2.Element("typ").Value, simpleQuery2.Element("orderdate").Value, simpleQuery2.Element("p4code").Value, simpleQuery2.Element("runtime").Value, ); } catch (NullReferenceException) { textBox1.Text = "did a stupid mistake mofo"; } hardcore++; }\[/code\]the information from "simpleQuery2.Element("id").Value" is a Value Stored in the MainParent.My Problem is now:I cant be sure which values/childs/grandchilds.. are inside the XML file.So to successfully import it into the MySQL i would need to import all the elements each by each, cause else i get a error and the import doesnt work.I need to know Parent/Valuename/Value or Parent/Child/Valuename/value.because Parent/Valuename/Value will be imported into the table "PARENT"and Parent/Child/Valuename/value will be imported to the table "CHILD".If i know Which Childs/Grandchilds are, i know what values to expect.I Already can get a full list of the element with:\[code\] XDocument xmlDoc2 = XDocument.Load("c:\\a.xml"); foreach (var name in xmlDoc2.Root.DescendantNodes().OfType<XElement>() .Select(x => x.Name).Distinct()) { textBox1.Text = textBox1.Text + "\r\n"+name; }\[/code\]
 
Back
Top