English 中文(简体)
使用VB.net从文档读取XML
原标题:Reading XML from document with VB.net
  • 时间:2010-02-22 16:23:05
  •  标签:
  • xml
  • vb.net

我想要能够读取我的XML文档,但我有些迷失其中。我不能在此处张贴我的XML,因为它只是尝试使用标记。无论如何,我有一个包围我想读取的整个对象的根节点。从那里,有一些元素。其中2个元素可能具有多个实例。在XML文档中,我只需要读取一个对象。

提前致谢。我希望我能足够清楚地解释,即使无法发布我的XML。

请将此翻译成中文:::

这是我目前的代码:

Private Function ExtractXMLFromFileToBonder(ByVal path As String) As Bonder
    Dim extractedBonder As New Bonder
    Dim settings As New XmlReaderSettings
    settings.IgnoreWhitespace = True

    settings.CloseInput = True

    Using reader As XmlReader = XmlReader.Create(path, settings)

        With reader

            .ReadStartElement("Machine_Name")
            MsgBox(.GetAttribute("Name"))

        End With

    End Using

    Return Nothing

End Function
问题回答

使用 System.xml 中的xml读取器来实现这一点。 您可以使用自己选择的xmlreader。 请参考 http://msdn.microsoft.com/en-us/library/system.xml%28VS.71%29.aspx 上的XML命名空间。

你也可以使用linq to xml。

教程:

将此翻译成中文:http://www.devcurry.com/2009/05/linq-to-xml-tut或者ials-that-make-sense.html 这篇文章主要介绍了Linq to XML的教程,并且对于初学者来说非常易懂。

或者

http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

或者 I recommend this book Linq in Action published by manning..

http://linqinaction.net/

做类似的事情

Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode

 Create the XML Document
m_xmld = New XmlDocument()

 Load the Xml file
m_xmld.Load("YourPath	est.xml")

 Show all data in your xml
MessageBox.Show(m_xmld.OuterXml)


 Get the list of name nodes
m_nodelist = m_xmld.SelectNodes("/family/name")

 Loop through the nodes
For Each m_node In m_nodelist
 Get the Gender Attribute Value
Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value

 Get the firstName Element Value
Dim firstNameValue = m_node.ChildNodes.Item(0).InnerText

 Get the lastName Element Value
Dim lastNameValue = m_node.ChildNodes.Item(1).InnerText

 Write Result to the Console
Console.Write("Gender: " & genderAttribute _
& " FirstName: " & firstNameValue & " LastName: " _
& lastNameValue)
Console.Write(vbCrLf)
Next




相关问题
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?

热门标签