Solved

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

Posted on 2014-02-21
9
373 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML XSL Message 3 14
how to read json value 2 31
Allow user to hide and show datagridview columns 4 17
Close form "before" open 3 28
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

862 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now