Solved

Parse xml for certain info from ftp logs, grab the entire section and display the info afterwards.

Posted on 2014-02-21
9
380 Views
Last Modified: 2014-03-01
I have started the following code to read through an entire directory for xml files and to read, identify, and display the info I need. If someone can help me pick out the parts I need from my xml that would be great.
For instance if the cmd is "get" I need to pull the entire entry, so ip, timestamp, user, etc... then add all of it nicely to a listbox for easy viewing.

Option Explicit On
Imports System.Xml
Imports System.IO
Public Class Form1

     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Opens Folder Browser ad populates textbox one upon selection.
        If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath
        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        For Each sPath As String In IO.Directory.GetFiles(Chr(34) & TextBox1.Text)

            If IO.Path.GetExtension(sPath) = ".xml" Then
                Using reader As XmlReader = XmlReader.Create(sPath)
                    While reader.Read
                        If reader.Name = "log" Then
                        ElseIf reader.Name = "entry" Then
                            ElseIf reader.
                        End If
                    End While
                End Using
           End If
        Next

    End Sub

End Class

Open in new window


And this is the xml info I am working with:
<?xml version="1.0" encoding="UTF-8"?>

-<log>
-<entry>
<log_time>20130422-00:01:01</log_time>
-<description>
<![CDATA[220 FTP]]>
</description>
<service>FTP</service>
<sessionid>34414127</sessionid>
<type>0</type>

<severity>1</severity>

<lstnconnaddr>192.168.0.21:21</lstnconnaddr>

<cliconnaddr>192.168.0.40:61848</cliconnaddr>

<cmd>start</cmd>

<errnum>220</errnum>

<sguid>C49576C6-9AFE-437E-A0B8-B47BAE307139</sguid>

</entry>
-<entry>
<log_time>20130422-00:01:01</log_time>
-<description>
<![CDATA[received]]>
</description>
<service>FTP</service>
<sessionid>34414127</sessionid>
<type>0</type>
<severity>1</severity>
<lstnconnaddr>192.168.0.21:21</lstnconnaddr>
<cliconnaddr>192.168.0.40:61848</cliconnaddr>
<cmd>USER</cmd>
<sguid>C49576C6-9AFE-437E-A0B8-B47BAE307139</sguid>
</entry>
-<entry>
<log_time>20130422-00:01:01</log_time>
-<description>
<![CDATA[Enter password]]>
</description>
<service>FTP</service>
<sessionid>34414127</sessionid>
<type>0</type>
<severity>1</severity>
<user>test</user>
<host>ftp</host>
<lstnconnaddr>192.168.0.21:21</lstnconnaddr>
<cliconnaddr>192.168.0.0:61848</cliconnaddr>
<cmd>USER</cmd>
+<params>
<errnum>331</errnum>
<sguid>C49576C6-9AFE-437E-A0B8-B47BAE307139</sguid>
</entry>

Open in new window

0
Comment
Question by:MarcViste
  • 3
  • 3
  • 2
9 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 39879899
Corrected XML example and formatted
<?xml version="1.0" encoding="utf-8"?>
<log>
  <entry>
    <log_time>20130422-00:01:01</log_time>
    <description><![CDATA[220 FTP]]></description>
    <service>FTP</service>
    <sessionid>34414127</sessionid>
    <type>0</type>
    <severity>1</severity>
    <lstnconnaddr>192.168.0.21:21</lstnconnaddr>
    <cliconnaddr>192.168.0.40:61848</cliconnaddr>
    <cmd>start</cmd>
    <errnum>220</errnum>
    <sguid>C49576C6-9AFE-437E-A0B8-B47BAE307139</sguid>
  </entry>
  <entry>
    <log_time>20130422-00:01:01</log_time>
    <description><![CDATA[received]]></description>
    <service>FTP</service>
    <sessionid>34414127</sessionid>
    <type>0</type>
    <severity>1</severity>
    <lstnconnaddr>192.168.0.21:21</lstnconnaddr>
    <cliconnaddr>192.168.0.40:61848</cliconnaddr>
    <cmd>USER</cmd>
    <sguid>C49576C6-9AFE-437E-A0B8-B47BAE307139</sguid>
  </entry>
  <entry>
    <log_time>20130422-00:01:01</log_time>
    <description><![CDATA[Enter password]]></description>
    <service>FTP</service>
    <sessionid>34414127</sessionid>
    <type>0</type>
    <severity>1</severity>
    <user>test</user>
    <host>ftp</host>
    <lstnconnaddr>192.168.0.21:21</lstnconnaddr>
    <cliconnaddr>192.168.0.0:61848</cliconnaddr>
    <cmd>USER</cmd>
    <params />
    <errnum>331</errnum>
    <sguid>C49576C6-9AFE-437E-A0B8-B47BAE307139</sguid>
  </entry>
</log>

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 39880078
@MarcViste

Please post a sample of XML that has at least one instance of a "get" command.  Do not copy/paste XML from your browser.  Copy/paste actual XML.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39880659
You might be able to use XML serialization to achieve what you need.

HOW TO: Serialize and Deserialize XML in Visual Basic .NET
https://support.microsoft.com/kb/316730/en-us
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Assisted Solution

by:MarcViste
MarcViste earned 0 total points
ID: 39882947
I have adjusted the code below. Please tell me why it's not returning anything into my listboxes.

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        For Each sPath As String In IO.Directory.GetFiles(TextBox1.Text)
            TextBox2.Text = sPath
            If IO.Path.GetExtension(sPath) = ".xml" Then

                Dim doc As New XmlDocument()
                Dim nodes As XmlNodeList
                doc.Load(sPath)

                nodes = doc.SelectNodes("/log")
                Dim node As XmlNode

                For Each node In nodes
                    Dim nodesentry As XmlNodeList
                    Dim node1 As XmlNode
                    nodesentry = doc.SelectNodes("/entry")
                    For Each node1 In nodesentry
                        Dim nodecliconnaddr As XmlNode = node.SelectSingleNode("cliconnaddr")
                        If nodecliconnaddr IsNot Nothing Then
                            ListBox1.Items.Add(nodecliconnaddr.InnerText)
                        End If

                        Dim nodecmd As XmlNode = node.SelectSingleNode("cmd")
                        If nodecmd IsNot Nothing Then
                            ListBox2.Items.Add(nodecmd.InnerText)
                        End If

                        Dim nodelog_time As XmlNode = node.SelectSingleNode("log_time")
                        If nodelog_time IsNot Nothing Then
                            ListBox3.Items.Add(nodelog_time.InnerText)
                        End If
                    Next
                Next
            End If

Open in new window

0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 39883165
Your XML hierarchy is log/entry, and you are selecting <entry> elements from document at the root level.  I believe that you should be selecting those elements from the "node". XmlNode.

Dim nodesentry As XmlNode = node.SelectSingleNode("entry")

Open in new window

0
 

Assisted Solution

by:MarcViste
MarcViste earned 0 total points
ID: 39883240
nodes = doc.SelectNodes("/log/entry") is what I needed.

Thanks guys.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39883287
If you are selecting <log> elements here:

nodes = doc.SelectNodes("/log")

Open in new window


then, you can use the relative XPath expression against node" in the for loop, that I showed you, instead of getting them from root /log/entry.
0
 

Author Closing Comment

by:MarcViste
ID: 39897030
Part of my comment was the solution.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question