Question How to transform XML file to Text file

Vincent

New member
Joined
Sep 25, 2008
Messages
3
Programming Experience
Beginner
Hi,
I need help.
I'm a student who is not very familiar with VB.Net. But i have to use VB.Net to extract XML file from a server and into a Text file.
The XML file will look like this (see below):

HTML:
<?xml version="1.0" encoding="utf-8" ?> 
<response> 
<TAG> 
<tagid>72057994675</tagid> 
<mac>00:10:C6:FD:BD:B3</mac> 
<posx>253</posx> 
<posy>587</posy> 
<posmodelid>96</posmodelid> 
<posmapid>0</posmapid> 
<poszoneid>-1</poszoneid> 
<posmapname>REP_Poly-Model 2</posmapname> 
<posquality>42</posquality> 
<posreason>3</posreason> 
<postime>1217758452921</postime> 
<postimestamp>2008-08-03 18:14:12+0800</postimestamp> 
<poscounter>284</poscounter> 
<battery>90</battery> 
</TAG> 
</response>

thanks.
 
VB.NET:
		Dim Doc As New XmlDocument
		Dim NodeList As XmlNodeList

		Doc.Load("C:\Temp\MyXml.xml")
		NodeList = Doc.SelectNodes("/response/TAG")
		Dim sw As New StreamWriter("C:\Temp\MyXmlContents.txt", False)

		For Each Node As XmlNode In NodeList
			For i As Integer = 0 To Node.ChildNodes.Count - 1
				With Node.ChildNodes.Item(i)
					sw.WriteLine(.Name & ": " & .InnerText)
				End With
			Next
		Next

		sw.Close()
 
VB.NET:
Public Function ReadXML(ByVal xmlFilePath As String, ByVal NodetoExtract As String, ByVal OutputFilePath As String, ByVal Delimter As Char, ByVal StrLogPath As String) As Boolean

        Try
            ' Open an XML file
            Dim reader As New XmlTextReader(xmlFilePath)
            '    reader.WhitespaceHandling = WhitespaceHandling.All
            reader.Namespaces = True
            While reader.Read
                If reader.NodeType = XmlNodeType.Element Then
                    '  If reader.Name = NodetoExtract Then
                    If String.Compare(reader.Name, NodetoExtract, True) = 0 Then

                        Dim Str As String = reader.ReadOuterXml                       '   Str = Replace(Str, "#", "")
                        '  Dim strbuildstringAs New System.Text.StringBuilder
                        Me.GotNode = True
                        Me.GetRecordCount = Me.GetRecordCount + 1
                        Dim XMLDoc As New XmlDocument

                        strbuildstring = New System.Text.StringBuilder
                        XMLDoc.LoadXml(Str)

                        Dim XMLEL As XmlElement
                        Dim Att As XmlAttribute

                        XMLEL = XMLDoc.DocumentElement


                        If XMLEL.HasAttributes Then
                            Dim i
                            For i = 0 To XMLEL.Attributes.Count - 1
                                Att = (XMLEL.Attributes(i))
                                strbuildstring.Append(Delimter & Att.Name() & Delimter & Att.Value() & Delimter)
                            Next
                        End If

                        If XMLEL.HasChildNodes Then
                            If XMLEL.ChildNodes(0).NodeType = XmlNodeType.Text Then
                                strbuildstring.Append(Delimter & XMLEL.Name & Delimter & (XMLEL.InnerText) & Delimter)
                            End If
                        End If
                        If XMLEL.HasChildNodes Then
                            Dim j
                            For j = 0 To XMLEL.ChildNodes.Count - 1
                                If XMLEL.ChildNodes(j).NodeType = XmlNodeType.Element Then
                                    GetAttributesValue(XMLEL.ChildNodes(j), Delimter)
                                End If
                            Next
                        End If

                        strbuildstring.Replace(Delimter & Delimter, Delimter)
                        WriteOutRecord(OutputFilePath, strbuildstring.ToString)
                    End If
                End If

            End While
        Catch e As Exception

            WriteOutLog(StrLogPath, Err.Description)
            Return False
        End Try
        Return True
    End Function
 
use VB.Net to extract XML file from a server and into a Text file
You can use System.Net.WebClient and its DownloadFile method to download the Xml from server address to a local file, the Xml is already plain text.
VB.NET:
Dim web As New Net.WebClient
web.DownloadFile(address, filename)
 
Hi JohnH,

thanks for your reply.

But when you say write the code as "Dim web As New Net.WebClient
web.DownloadFile(address, filename) "


the address i have is : http://127.0.0.1:8550/epe/pos/taglist?name=RPTag&fields=all
<-- it is directly from the server.

may i know what is the "file name"? do i have to save the xml in my computer or the VB.Net can extract the information real-time from the server?

Thanks.

PS: I am still trying out the solutions given in the other replies. Thanks MattP and bharath639.
 
Vincent, I misunderstood your post and throught you were wanting to parse the contents of an XML file to a text file.

JohnH's answer is correct. Maybe it would help if it were stated like:

VB.NET:
web.DownloadFile(URL, DestinationFile)
 
Hi MattP,

i'm not very familiar with technical terms, may i know what is the "destination file"
that you have mentioned "web.DownloadFile(URL, DestinationFile) ".

do we have to create a folder to save the constantly updating XML files?-thus a destination file?
also, to obtain the XML file from the server, i have to actually login into the server with a password. Will this create more complications?

thanks.
 
Back
Top