Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-02-21
9
Medium Priority
?
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
9 Comments
 
LVL 46

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 46

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 2000 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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