Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.NET - Reading values from a XML File and outputing the xml values depending on what is used in the file name

Posted on 2013-06-10
7
Medium Priority
?
310 Views
Last Modified: 2013-06-12
Good afternoon,

I'm using Visual Studio Express 2012 for my VB.Net windows form project.

I need some help with my application reading values from an xmlfile and outputting this information to a file depending on the filenames code. I have no idea how to accomplish this and would appreciate any sample to get me started.

The file names will always be:

AccNum-YYYYMMDD-code

Example: 123456-20130610-FTO

My xml file is below. Please note that these values are NOT static.

<Settings>
  <ApplicationSettings>
    <code1>FTO</code1>
    <docgroup1>Operations</docgroup1>
    <doctype1>Funds Transfer</doctype1>
    <docsubtype1>Out</docsubtype1>
    <code2>TBA</code2>
    <docgroup2>Operations</docgroup2>
    <doctype2>Funds Transfer</doctype2>
    <docsubtype2>Reversed</docsubtype2>
    <code3>ACL</code3>
    <docgroup3>Documentation</docgroup3>
    <doctype3>Client Documentation</doctype3>
    <docsubtype3>Termination</docsubtype3>
    <code4>FTI</code4>
    <docgroup4>NULL</docgroup4>
    <doctype4>NULL</doctype4>
    <docsubtype4>NULL</docsubtype4>
  </ApplicationSettings>
</Settings>

Open in new window


Below is my code where “docgroup”, “doctype” and “docsubtype” is where the values of the xml file will go. But this all depending on what code is in the file name.

Dim str As String
        For Each rfiles As String In System.IO.Directory.GetFiles(destdir)

            randomnumber = randomclass.Next(10000, 99999)

            Dim formattedDate As String = GetFormattedDateFromFileName(rfiles)

            str = str & rfiles & "|" & System.IO.Path.GetFileNameWithoutExtension(rfiles).Split("-")(0).Trim & "|" & "_" & "||" & "docgroup" & "|" & "doctype" & "|" & "docsubtype" & "|" & "swfoi" & randomnumber & "|" & formattedDate & "|" & Environment.NewLine

        Next

        Dim outputname As String = [String].Format("\\ServerA\ITDept\swfoi{0}.txt", DateTime.Now.ToString("MMddyyyyhhmmss"))
        System.IO.File.WriteAllText(outputname, str)

    End Sub

Open in new window


So for example some of the file names in the directory are:

12345-20130610-FTO Reviewed and scanned.pdf
54265-20130512-FTI A1.pdf
45752-20121204-TBA.pdf

The additional code that I have no idea how to do should look at the file name and output a the correct "docgroup" "doctype" and "docsubtype" for each file, end result would look like below:

\\ServerA\ITDept\files\12345-20130610-FTO Reviewed and scanned.pdf|12345|_|| Operations| Funds Transfer|Out|swfoi6848484|06/10/2013|
\\ServerA\ITDept\files\54265-20130512-FTI A1.pdf|54265|_|| NULL| NULL|NULL|swfoi15157|05/12/2013|
\\ServerA\ITDept\files\45752-20121204-TBA.pdf|45752|_|| Operations| Funds Transfer|Reversed|swfoi54572258|12/04/2012|

Open in new window


Is anyone able to provide some sample code on how to accomplish this with the information I have provided?

Kindly advise.

Regards,
N
0
Comment
Question by:nobushi
[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
  • 4
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 39236306
The below code should help you out. Some more checking while reading the xml would be nice perhaps (actually the structure of the xml file should be changed if at all possible).
Public Class Form1

    Structure CodeInfo
        Dim docgroup As String
        Dim doctype As String
        Dim docsubtype As String
    End Structure

    Dim destdir As String = "X:\EE\Q_28153249\files\"
    Dim randomnumber As Integer
    Dim randomclass As New Random()
    Dim dctCodes As New Dictionary(Of String, CodeInfo)
    Dim xmlDoc As New System.Xml.XmlDocument

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        LoadDocInfo()
        ProcessFiles()

    End Sub

    Private Sub LoadDocInfo()
        xmlDoc.Load("Settings.xml")
        Dim strCode As String = ""
        Dim inf As New CodeInfo
        For Each xmlNode As System.Xml.XmlNode In xmlDoc.DocumentElement.SelectSingleNode("ApplicationSettings").ChildNodes
            If xmlNode.Name.Substring(0, 4) = "code" Then
                strCode = xmlNode.InnerText
            ElseIf xmlNode.Name.Substring(0, 8) = "docgroup" Then
                inf.docgroup = xmlNode.InnerText
            ElseIf xmlNode.Name.Substring(0, 7) = "doctype" Then
                inf.doctype = xmlNode.InnerText
            ElseIf xmlNode.Name.Substring(0, 10) = "docsubtype" Then
                inf.docsubtype = xmlNode.InnerText
                dctCodes.Add(strCode, inf)
                strCode = ""
                inf = New CodeInfo
            End If
        Next
    End Sub

    Private Sub ProcessFiles()

        Dim str As String = String.Empty
        For Each rfiles As String In System.IO.Directory.GetFiles(destdir)

            randomnumber = randomclass.Next(10000, 99999)

            Dim strFileNameParts() As String = System.IO.Path.GetFileNameWithoutExtension(rfiles).Split("-")

            Dim formattedDate As String = GetFormattedDateFromFileName(strFileNameParts(1))

            Dim strCode As String = strFileNameParts(2).Substring(0, 3) ' or split at space?

            str = str & rfiles & "|" & strFileNameParts(0).Trim & "|" & "_" & "||" & dctCodes(strCode).docgroup & "|" & dctCodes(strCode).doctype & "|" & dctCodes(strCode).docsubtype & "|" & "swfoi" & randomnumber & "|" & formattedDate & "|" & Environment.NewLine

        Next

        Dim outputname As String = [String].Format("\\ServerA\ITDept\swfoi{0}.txt", DateTime.Now.ToString("MMddyyyyhhmmss"))
        System.IO.File.WriteAllText(outputname, str)

    End Sub

    Function GetFormattedDateFromFileName(ByVal strDateFromFileName As String)
        Return String.Format("{1}/{2}/{0}", strDateFromFileName.Substring(0, 4), strDateFromFileName.Substring(4, 2), strDateFromFileName.Substring(6, 2))
    End Function

End Class

Open in new window

0
 
LVL 1

Author Comment

by:nobushi
ID: 39237894
Thanks Robert,

I have a looksee at this and let you know if i have any questions.

BTW, When you say the structure of my xml file should change, what do you mean by this?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39238085
The structure of xml as I've seen it is often more like this (one of many possibilities):
<Settings>
  <ApplicationSettings>
    <ApplicationSetting>
      <code>FTO</code>
      <docgroup>Operations</docgroup>
      <doctype>Funds Transfer</doctype>
      <docsubtype>Out</docsubtype>
    </ApplicationSetting>
    <ApplicationSetting>
      <code>TBA</code>
      <docgroup>Operations</docgroup>
      <doctype>Funds Transfer</doctype>
      <docsubtype>Reversed</docsubtype>
    </ApplicationSetting>
    <ApplicationSetting>
      <code>ACL</code>
      <docgroup>Documentation</docgroup>
      <doctype>Client Documentation</doctype>
      <docsubtype>Termination</docsubtype>
    </ApplicationSetting>
    <ApplicationSetting>
      <code>FTI</code>
      <docgroup>NULL</docgroup>
      <doctype>NULL</doctype>
      <docsubtype>NULL</docsubtype>
    </ApplicationSetting>
  </ApplicationSettings>
</Settings>

Open in new window

So the repetitions are not numbered but grouped by an extra level and named the same otherwise. Node names don't have to be unique.

Of course the code would have to be altered if you were to make that change. It would get simpler on the level of not having to look at part of the node name anymore.

In either case, it might be necessary to think about a 'rule' for missing info.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Closing Comment

by:nobushi
ID: 39238331
Thanks for the startoff with the xml file code Robert.

And with the restructure of the xml file, I did see other examples like the one you gave but i guess the reason why i didnt use them was that I didnt quite understand why they would not be unique (numbered like i have them now). I'm still thinking of an ini file how things were unique in there. But im slowly understanding how this would be possible with the right code.

Seems I cant really find any good documenation on this on the net. Would you happen to know of a url that would explain this more to me?

Regarding what you mentioned with a rule to identify null values in my xml file. Would it be possible for you to provide me with an example on how this would be done?

I dont like having the value of null in there and if my program could identify that nothing is in those nodes without me having to put anything in there that would be a lot better.

Thanks,
N
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39239941
I'm not sure if there's a "catch-all" example for an xml structure like this. Like I said, what I posted is one of many possible structures. It depends on whether you want to make a structure fit for human reading (and editing) or more for machine reading and efficiency. In this case it would seem to me that a trade-off between efficiency and readability should be fine. If you can let go of the uniqueness of the node names a structure like I posted should be ok. With a 'rule' I just meant what to do if one of the nodes is missing. Do you want to generate an error or just use NULL or empty strings. Don't read too much into that ;-)
0
 
LVL 1

Author Comment

by:nobushi
ID: 39240914
Thank you for your answer regarding the xml structure. For now I think I will leave as is with this, until I become more familiar with it.

As for the rule I would rather use empty strings without having the need to put NULL in there when its not in use. How would i go about to set something up where my application will just ignore empty values in the xml file?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39241990
There's a couple of things that can happen. Suppose a file exists with code FTI but that code doesn't exist in the xml file, what needs to be written to the output? Same for a code that exists in the xml but doesn't have all 3 values defined for example. That is what I mean with rules. Some checking can be easily added to the code for example:
        For Each rfiles As String In System.IO.Directory.GetFiles(destdir)

            randomnumber = randomclass.Next(10000, 99999)

            Dim strFileNameParts() As String = System.IO.Path.GetFileNameWithoutExtension(rfiles).Split("-")

            Dim formattedDate As String = GetFormattedDateFromFileName(strFileNameParts(1))

            Dim strCode As String = strFileNameParts(2).Substring(0, 3) ' or split at space?

            Dim dg As String = String.Empty
            Dim dt As String = String.Empty
            Dim ds As String = String.Empty

            If dctCodes.ContainsKey(strCode) Then
                dg = dctCodes(strCode).docgroup
                dt = dctCodes(strCode).doctype
                ds = dctCodes(strCode).docsubtype
            End If

            If dg.Equals(String.Empty) Then dg = "<empty>"
            If dt.Equals(String.Empty) Then dt = "<nothing>"
            If ds.Equals(String.Empty) Then ds = "<nada>"

            str = str & rfiles & "|" & strFileNameParts(0).Trim & "|" & "_" & "||" & dg & "|" & dt & "|" & ds & "|" & "swfoi" & randomnumber & "|" & formattedDate & "|" & Environment.NewLine

        Next

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

664 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