English 中文(简体)
v
原标题:xml parsing in vb.net

我有一份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

我只能说明如何获得子宫的数量,然后如何在继承时相互提及,以获得该子的名声和属性。 任何建议都将受到高度赞赏。

最佳回答

如果你想利用XmlTextReader,那么你可以通过利用“Rembra”解决你的问题:

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,则会比较容易。

问题回答

你的评论之一表明,你正在利用3.5框架。 如果是这样,你就可以利用XML字面来找到解决办法。

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

我过去为此做了些什么,这就是说,“愿意”取而代之。 每次阅读之后,都检查起算点。 如果这不是一个开端要素,那么你就到了封闭的原主的末端,现在就到了下一个阶段。 该文件中的样本代码可能有助于:

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

XmlReader不能给你以目前要素的子数,因为它在某个时候就读了一个标签。 它没有读到其他内容,但它不知道有多少。 如果你想在阅读XML树时获得更多的信息,则使用XmlDocument。 然而,XmlDocument在你开始处理之前将一度读到整个档案中,而XmlReader则从一开始即读完文件。 XmlReader应当更快、更高效的记忆。

这里的样本表明,如果你不事先知道某一要素会具有哪些特性的话,如何改变属性:

http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.movetonextattribute.aspx”rel=“nofollow noreferer”>http://msdn.microsoft.com/en-us/library/system.xmlreader.movetonextattribute.aspx





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签