?
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
?
397 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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