我得到了一個像這樣的XML文件。LINQ to XML從文件中獲取所有節點名稱
Parent
-Value
-Value
-Child
--Value
--Value
---Grandchild
----Value
---GrandchildEND
-ChildEND
-Value
-Child2
......
XML文件的結構是可變的。意思是每個XML有不同的Childs,Grandchilds等。 我想打開XML文件,並將XML文件中的數據輸入到MYSQL數據庫。
這裏的代碼我用來打開XML並將信息輸入到MySQL。
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++;
}
來自「simpleQuery2.Element(」id「)。」Value「的信息是存儲在MainParent中的值。
我現在的問題是:
我不能肯定哪些值/孩子的/ grandchilds ..是XML文件中。 因此,要成功地將其導入到MySQL中,我需要每個導入所有元素,因爲我得到一個錯誤,導入不起作用。
我需要知道父/ Valuename /值或父/子/ Valuename /值。 ,因爲父/ Valuename /值將被導入到表「父母」 和父/子/ Valuename /值將被導入到表「CHILD」。
如果我知道哪些Childs/Grandchilds,我知道什麼樣的值。
我已經可以得到元素的一個完整的列表:
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; }
所以[你嘗試過什麼(http://mattgemmell.com/2008/12/08/what-have-you-tried/)做了類似的問題在代碼中(請告訴我們的代碼)? –
你需要明確你的要求。向我們展示XML。告訴我們你想擺脫它。告訴我們你的嘗試。 – yamen
我試圖用xelement得到節點列表,但是這種方法沒有解決。因爲它告訴我xmlDoc2.Attributes()是不可能的。抱歉不能張貼XML,因爲我沒有實際上一個。我剛剛得到了結構的描述。 – mahxds