我有一个xml格式的文档,如下所示:
<?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 :(得分:4)
您的一条评论表明您使用的是3.5框架。如果是这样,那么您可以利用XML文字来获得解决方案。
Dim data = XDocument.Load(xmlReader)
Dim count = data.<Ingredients>.Elements().Count()
答案 1 :(得分: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 to XML会更容易。
答案 2 :(得分:0)
我过去所做的就是调用Read()来依次获取每个子元素。在每次Read()之后,检查它是否为start元素。如果它不是一个开始元素,那么你已经到了封闭的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