Write XML File contents into Text File

furnitureheaven

Registered User.
Local time
Today, 10:11
Joined
Aug 5, 2008
Messages
36
Hi,
I am new in .Net, so i really don't know much.
I have a file in XML (with Parent Nodes and Chiled nodes) in it. I want to write a contents from XML File to text file in VB Consoule Application. i have done some code, but its give me the following error. Could anyone guide.

Error:-

1 'Sub Main' was not found in 'ConsWriteTxt.Module1'.
2 Statement is Not Valid in a Namespace.

My code is this

Code:
Imports system.IO
Imports System.Xml

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("C:\Note.xml")
        '    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("C:\Atest.txt", strbuildstring.ToString)
                End If
            End If
        End While
    Catch e As Exception
        WriteOutLog(StrLogPath, Err.Description)
        Return False
    End Try
    Return True
End Function
 

Users who are viewing this thread

Back
Top Bottom