2013-04-28 109 views
-3

我的問題是與Xml文件,看看: 這是行不通的。XML所有元素列表

IEnumerable<XElement> stepsList = doc.Elements(); 

XML文件

<OneGoal> 
<Goal>100000</Goal> 
<StepOne>ewewewe</StepOne> 
<StepTwo>wee</StepTwo> 
<StepThree>MY</StepThree> 
<StepFour>wwwww</StepFour> 
<StepFive>ddddw</StepFive> 
<StepSix>fcd</StepSix> 
<StepSeven>blblblfl</StepSeven> 
<StepEight>z dwadddddsssssssssssss</StepEight> 
<StepNine>radwds</StepNine> 
<StepTen> 
blblblblblblb 
</StepTen> 
<DateDay>18</DateDay> 
<DateMonth>7</DateMonth> 
<DateYear>2019</DateYear> 
</OneGoal> 

我想IEnumberable所有元素。早些時候所有的元素都有名字「step」。

+0

你能顯示你想要的輸出嗎? – 2013-04-28 18:56:45

+0

第一個元素具有所有文件xml。 – MichaelS 2013-04-28 18:58:38

+0

你能告訴我們'doc'是如何創建的嗎?如果它是'XDocument'類型,那麼你的代碼應該是有效的。 – 2013-04-28 19:01:44

回答

2

如何將您的平板xml轉換爲Dictionary<string,string>

var dict = XDocument.Parse(xml) 
      .Element("OneGoal") 
      .Elements() 
      .ToDictionary(e => e.Name.LocalName, e => e.Value); 

Console.WriteLine(dict["StepOne"]); 
+0

這就是它!不錯的選擇。 – MichaelS 2013-04-28 19:05:27

1

使用XDocument從文件加載或從字符串解析。

var stepList = doc.Descendants(); 
+0

這很好,@ L.B有更好的方法,thx! – MichaelS 2013-04-28 19:06:32