2009-12-12 68 views
0

我有一个XML格式的文档,看起来像这样:XML在vb.net解析

<?xml version="1.0" encoding="windows-1250"?> 
< Recipe> 
    < Entry name="Stuffed Red Cabbage" ethnicity="Slavic" /> 
    < Cook_Time Hrs="1" Mins="30" /> 
    < Ingredients> 
       < Cabbage Amount="1" Measurement="head" /> 
       < Egg Amount="1" Measurement="unit" /> 
       < Ground_Beef Amount="1" Measurement="lb" /> 
       < Margarine Amount="1/2" Measurement="cup" /> 
       < Onion Amount="1" Measurement="unit" /> 
       < Rice Amount="1" Measurement="cup" /> 
       < Tomato_Soup Amount="3" Measurement="cans" /> 
    < /Ingredients> 
    < Description>core cabbage and boil until leaves start pulling away. Strip leaves and let cool. 
chop onion and place in frying pan with margarine and heat till lightly browned. 
put ground beef, rice, onion, egg and salt to taste in bowl and mix. 
stuff each leaf with mixture. 
put tomato soup and stuffed leaves in pot and cook for about an hour.</Description> 
</Recipe> 

,我有代码,到目前为止是这样的:

OpenFileDialog1.Filter = "RecipeBook files (*.rcp)|*.rcp" 
    If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then 
     Try 
      Dim settings As New XmlReaderSettings() 
      settings.IgnoreComments = True 
      Dim RecipeCard As String = OpenFileDialog1.FileName 
          Dim xmlreader As XmlTextReader 
      xmlreader = New XmlTextReader(RecipeCard) 
      Do While xmlreader.Read 
       'needs to read xml and write appropriate items to database and listview 
       xmlreader.MoveToContent() 
       If xmlreader.Name.Equals("Entry") Then 
        MessageBox.Show(xmlreader.GetAttribute("name") & " " & xmlreader.GetAttribute("ethnicity"), "test") 
       End If 
       If xmlreader.Name.Equals("Cook_Time") Then 
        MessageBox.Show(xmlreader.GetAttribute("Hrs") & " hrs " & xmlreader.GetAttribute("Mins") & " mins", "test") 
       End If 
       If xmlreader.Name.Equals("Ingredients") Then 

       End If 
      Loop 
     Catch 
     End Try 
    End If 

我的问题必须做解析配料部分。我打算做这样的事情:

Dim IngredientCount As Integer = 0 
Dim count As Integer = (something here that gets the count of subelements inside the Ingredients element) 
        For i = 1 To count 
        MessageBox.Show(xmlreader.GetAttribute("Amount") & " " & xmlreader.GetAttribute("Measurement"), "test") 
        Next 

我只是无法弄清楚如何让子元素的数量,再怎么是指每一个连续获得的名称,然后那属性子元素。任何建议将不胜感激。

+0

您使用.net 3.5吗? Xlinq将使这更容易。 http://msdn.microsoft.com/en-us/vbasic/bb738050.aspx – 2009-12-12 19:58:29

+0

我使用3.5,我需要重用3.5紧凑,以及我会看看。 – MaQleod 2009-12-12 20:02:59

回答

1

如果你想使用XmlTextReader,那么你可以通过使用ReadSubtree解决您的问题:

If xmlreader.Name.Equals("Ingredients") Then 
    Dim inner As XmlReader 
    inner = xmlreader.ReadSubtree() 
    inner.Read() 
    While inner.Read 
     If inner.IsStartElement Then 
      MessageBox.Show(inner.GetAttribute("Amount") & " " & inner.GetAttribute("Measurement"), "test") 
     End If 
    End While 
End If 

它会更容易,虽然不使用XmlTextReader的,而是使用LINQ到XML。

+0

这个选项对我已有的所有东西都是最好的。就内存而言,它并不是最高效的,但它是最容易实现的。 – MaQleod 2009-12-13 23:14:13

0

我在过去所做的是调用Read()来依次获取每个子元素。在每个Read()之后,检查它是否是一个开始元素。如果它不是开始元素,那么您已经到达封闭式Ingredient标签的末尾,现在该阅读下一个。本文档中的示例代码可能会有所帮助:

http://msdn.microsoft.com/en-us/library/xaxy929c.aspx

的XmlReader不能给你当前元素的子元素的数量,因为它一次读取一个标签。它还没有读过其他元素,所以它不知道有多少元素。如果您想在阅读XML树时获得更多信息,请使用XmlDocument。但是,在你开始处理它之前,XmlDocument会一次将整个文件读入内存,而XmlReader会在你处理它时从头到尾读取文件。 XmlReader应该更快,更具有内存效率。

的这里示例演示如何通过属性迭代,如果你不知道提前哪些属性的元素将有:

http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.movetonextattribute.aspx

4

你的一个评论表明您使用的是3.5框架。如果是这样,那么你可以利用XML文字来获得你的解决方案。

Dim data = XDocument.Load(xmlReader) 
Dim count = data.<Ingredients>.Elements().Count() 
+0

Thaaaaaank为您最终使用XML文字...没有人似乎知道或使用它们;)+1 – Dario 2009-12-12 20:29:29