i have a windows app in vb.net , i am trying to read a xml file from my server(http://dev2010.abc.com/abc.xml).I am able to read abc.xml . now i have a requirement to show abc.xml in a rich text box in vb.net and then i have to show node and its value in combobox. I am able to do this with a directory(suppose that file is in C:/abc.xml) Please suggest me the way to the way开发者_如何学编程 to load that xml file from server in a rich tex box and from there show the nodes and corresponding value in a combobox and display its output in a textbox..
Code in VB .NET
Imports System.Xml
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents txtFile As System.Windows.Forms.TextBox
Friend WithEvents txtResults As System.Windows.Forms.TextBox
Friend WithEvents btnList As System.Windows.Forms.Button
Friend WithEvents txtTagName As System.Windows.Forms.TextBox
Friend WithEvents lblFile As System.Windows.Forms.Label
Friend WithEvents lblTag As System.Windows.Forms.Label
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents DG_IPInfo As System.Windows.Forms.DataGridView
Private components As System.ComponentModel.Container
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lblTag = New System.Windows.Forms.Label
Me.txtResults = New System.Windows.Forms.TextBox
Me.btnList = New System.Windows.Forms.Button
Me.lblFile = New System.Windows.Forms.Label
Me.txtFile = New System.Windows.Forms.TextBox
Me.txtTagName = New System.Windows.Forms.TextBox
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.DG_IPInfo = New System.Windows.Forms.DataGridView
CType(Me.DG_IPInfo, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'lblTag
'
Me.lblTag.Location = New System.Drawing.Point(47, 30)
Me.lblTag.Name = "lblTag"
Me.lblTag.Size = New System.Drawing.Size(32, 16)
Me.lblTag.TabIndex = 4
Me.lblTag.Text = "Tag"
'
'txtResults
'
Me.txtResults.Location = New System.Drawing.Point(496, 239)
Me.txtResults.Multiline = True
Me.txtResults.Name = "txtResults"
Me.txtResults.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.txtResults.Size = New System.Drawing.Size(200, 309)
Me.txtResults.TabIndex = 3
'
'btnList
'
Me.btnList.Location = New System.Drawing.Point(228, 30)
Me.btnList.Name = "btnList"
Me.btnList.Size = New System.Drawing.Size(40, 23)
Me.btnList.TabIndex = 1
Me.btnList.Text = "List"
'
'lblFile
'
Me.lblFile.Location = New System.Drawing.Point(82, 217)
Me.lblFile.Name = "lblFile"
Me.lblFile.Size = New System.Drawing.Size(100, 19)
Me.lblFile.TabIndex = 5
Me.lblFile.Text = "File"
Me.lblFile.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'txtFile
'
Me.txtFile.Location = New System.Drawing.Point(12, 239)
Me.txtFile.Multiline = True
Me.txtFile.Name = "txtFile"
Me.txtFile.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.txtFile.Size = New System.Drawing.Size(224, 309)
Me.txtFile.TabIndex = 2
'
'txtTagName
'
Me.txtTagName.Location = New System.Drawing.Point(100, 30)
Me.txtTagName.Name = "txtTagName"
Me.txtTagName.Size = New System.Drawing.Size(104, 20)
Me.txtTagName.TabIndex = 0
Me.txtTagName.Text = "Status"
'
'ComboBox1
'
Me.ComboBox1.FormattingEnabled = True
Me.ComboBox1.Location = New System.Drawing.Point(496, 179)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(200, 21)
Me.ComboBox1.TabIndex = 6
'
'DG_IPInfo
'
Me.DG_IPInfo.BackgroundColor = System.Drawing.SystemColors.ControlLightLight
Me.DG_IPInfo.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DG_IPInfo.Location = New System.Drawing.Point(12, 73)
Me.DG_IPInfo.Name = "DG_IPInfo"
Me.DG_IPInfo.Size = New System.Drawing.Size(461, 127)
Me.DG_IPInfo.TabIndex = 7
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(833, 560)
Me.Controls.Add(Me.DG_IPInfo)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.txtResults)
Me.Controls.Add(Me.txtTagName)
Me.Controls.Add(Me.lblTag)
Me.Controls.Add(Me.btnList)
Me.Controls.Add(Me.lblFile)
Me.Controls.Add(Me.txtFile)
Me.Name = "Form1"
Me.Text = "GetElementsByTagName"
CType(Me.DG_IPInfo, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Private xml_doc As XmlDocument
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim resolver As New XmlUrlResolver()
Dim myUri As New Uri("http://dev2010.abc.com/abc.xml")
' Dim myUri As New Uri("http://localhost/test_abc/abc.xml")
'Dim s1 As String = DirectCast(resolver.GetEntity(myUri, Nothing, GetType(String)), String)
Dim s As Stream = DirectCast(resolver.GetEntity(myUri, Nothing, GetType(Stream)), Stream)
Dim xmlTextReader As New XmlTextReader(s)
Dim xdoc1 As New XmlDataDocument()
xdoc1.DataSet.ReadXml(xmlTextReader, XmlReadMode.Auto)
Dim ds As DataSet = xdoc1.DataSet
'DG_LiveRates.DataSource = xdoc1.DataSet;
'DataSet ds = new DataSet();
Dim dt As DataTable = ds.Tables(0)
DG_IPInfo.DataSource = dt
Dim file_name As String = DataSubdirectory() & "\abc.xml"
'Dim results As String
txtFile.Text = GetFileContents(file_name)
xml_doc = New XmlDocument()
xml_doc.Load(file_name)
ComboBox1.Items.Add("Status")
ComboBox1.Items.Add("Ip")
ComboBox1.Items.Add("CountryCode")
ComboBox1.Items.Add("CountryName")
ComboBox1.Items.Add("RegionCode")
ComboBox1.Items.Add("RegionName")
ComboBox1.Items.Add("City")
ComboBox1.Items.Add("ZipCode")
ComboBox1.Items.Add("Latitude")
ComboBox1.Items.Add("Longitude")
End Sub
Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
'Dim xml_node_list As XmlNodeList
'Dim xml_node As XmlNode
' Dim results As String
'xml_node_list = xml_doc.GetElementsByTagName(txtTagName.Text)
'For Each xml_node In xml_node_list
'results = results & xml_node.InnerText & vbCrLf
'ComboBox1.Items.Add("S")
'Next xml_node
'txtResults.Text = results
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
Dim wid As Integer
Dim hgt As Integer
wid = ClientSize.Width \ 2
hgt = ClientSize.Height - txtFile.Location.Y
If hgt < 10 Then hgt = 10
lblFile.SetBounds(0, 0, wid, lblFile.Size.Height)
txtFile.SetBounds(0, txtFile.Location.Y, wid, hgt)
lblTag.SetBounds(wid, 0, lblTag.Size.Width, lblTag.Size.Height)
txtResults.SetBounds(wid, txtFile.Location.Y, wid, hgt)
btnList.SetBounds(ClientSize.Width - btnList.Size.Width, 0, btnList.Size.Width, btnList.Size.Height)
wid = btnList.Location.X - lblTag.Location.X - lblTag.Size.Width - 10
If wid < 10 Then wid = 10
txtTagName.SetBounds(lblTag.Location.X + lblTag.Size.Width, 0, wid, txtTagName.Size.Height)
End Sub
Private Sub txtFile_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFile.TextChanged
End Sub
Private Sub txtResults_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtResults.TextChanged
End Sub
Private Sub txtTagName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTagName.TextChanged
End Sub
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
Dim xml_node_list As XmlNodeList
Dim xml_node As XmlNode
Dim results As String
xml_node_list = xml_doc.GetElementsByTagName(txtTagName.Text)
For Each xml_node In xml_node_list
results = results & xml_node.InnerText & vbCrLf
Next xml_node
txtResults.Text = results
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim xml_node_list As XmlNodeList
Dim xml_node As XmlNode
Dim results1 As String
Dim results As String
results1 = ComboBox1.Text
xml_node_list = xml_doc.GetElementsByTagName(results1)
For Each xml_node In xml_node_list
results = results & xml_node.InnerText & vbCrLf
Next xml_node
txtResults.Text = results
End Sub
Private Sub DG_IPInfo_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG_IPInfo.CellContentClick
End Sub
Private Sub lblFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblFile.Click
End Sub
End Class
Please suggest me some way.
Me.lblTag.Location = New System.Drawing.Point(47, 30) Me.lblTag.Name = "lblTag" Me.lblTag.Size = New System.Drawing.Size(32, 16) Me.lblTag.TabIndex = 4 Me.lblTag.Text = "Tag"
精彩评论