Link to home
Start Free TrialLog in
Avatar of Kevin
KevinFlag for United States of America

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kevin

ASKER

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?
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.
Avatar of Kevin

ASKER

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
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 ;-)
Avatar of Kevin

ASKER

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