Solved

VB.net - How do I change my code to write to my new xml format

Posted on 2013-06-28
13
281 Views
Last Modified: 2013-07-15
Good afternoon,

I need help to re-write my VB.net code to write to my new xml structure. I have figured out how to read from it, I just need help on how to write to it now.

My XML structure before was like this:

<?xml version="1.0" encoding="utf-8"?>
<!--MyProgram Settings-->
<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


And the code I used to write to it was like this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim settings As New XmlWriterSettings()
        settings.Indent = True

        ' Initialize the Xmlwriter.
        Dim xmlwrt As XmlWriter = XmlWriter.Create(xmlfile, settings)

        With xmlwrt
            'Write the Xml declaration.
            .WriteStartDocument()
            'Write a comment.
            .WriteComment("MyProgram Settings")
            'Write the root element.
            .WriteStartElement("Settings")
            'Start our first report.
            .WriteStartElement("ApplicationSettings")

            'The settings nodes.

            .WriteStartElement("code")
            .WriteString(CDtxtbx1.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("docgroup")
            .WriteString(DGtxtbx1.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("doctype")
            .WriteString(DTtxtbx1.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("docsubtype")
            .WriteString(DSTtxtbx1.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("code")
            .WriteString(CDtxtbx2.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("docgroup")
            .WriteString(DGtxtbx2.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("doctype")
            .WriteString(DTtxtbx2.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("docsubtype")
            .WriteString(DSTtxtbx2.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("code")
            .WriteString(CDtxtbx3.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("docgroup")
            .WriteString(DGtxtbx3.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("doctype")
            .WriteString(DTtxtbx3.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("docsubtype")
            .WriteString(DSTtxtbx3.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("code")
            .WriteString(CDtxtbx4.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("docgroup")
            .WriteString(DGtxtbx4.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("doctype")
            .WriteString(DTtxtbx4.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("docsubtype")
            .WriteString(DSTtxtbx4.Text.ToString())
            .WriteEndElement()

            'The end of this settings.
            .WriteEndElement()

            'The end of this settings.
            .WriteEndElement()

            'Close the XmlTextwriter.
            .WriteEndDocument()
            .Close()

            MsgBox("Settings saved successfully!", MsgBoxStyle.Information, "Settings")

        End With

    End Sub

Open in new window


Since then I have changed my xml structure to look like this:

<?xml version="1.0" encoding="utf-8"?>
<!--MyProgram Settings-->
<Settings>
  <ApplicationSettings>
    <code id="FTO">
      <docgroup>Operations</docgroup>
      <doctype>Funds Transfer</doctype>
      <docsubtype>Out</docsubtype>
    </code>
    <code id="FTI">
      <docgroup>null</docgroup>
      <doctype>null</doctype>
      <docsubtype>null</docsubtype>
    </code>
    <code id="ACL">
      <docgroup>Documentation</docgroup>
      <doctype>Client Documentation</doctype>
      <docsubtype>Termination</docsubtype>
    </code>
    <code id="TBA">
      <docgroup>Operations</docgroup>
      <doctype>Funds Transfer Credit</doctype>
      <docsubtype>Reversed</docsubtype>
    </code>
  </ApplicationSettings>
  </Settings>

Open in new window


Can someone please help me with some new code for the new xml structure?

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
  • 7
  • 6
13 Comments
 
LVL 5

Expert Comment

by:Jason Schlueter
ID: 39285214
I might be mixing VBA with VB here but I don't think that it matters too much.  Give something like this a try.  It's tough to do in a text editor but it might put you on the right track.

Sub Button1_Click(sender As Object, e as EventArgs) Handles Button1.Click

Dim settings As New XmlWriterSettings()
settings.Indent = True

' Initialize the Xmlwriter.
Dim xmlwrt As XmlWriter = XmlWriter.Create(xmlfile, settings)
Dim i As Integer
Dim DGtextBox As String
Dim DTtextBox As String
Dim DStextBox As String
i = 1

'assuming that these are the only
'four codes - if not, you'll have to
'create a loop to insert all of the codes
'into the dictionary

Set dict = New Dictionary
		With dict
				.add key:="FTO", item:=1
				.add key:="FTI", item:=2
				.add key:="ACL", item:=3
				.add key:="TBA", item:=4

		With xmlwrt
				.WriteStartDocument()
				.WriteComment ("MyProgramSettings")
				.WriteStartElements ("Settings")
				.WriteStartElements ("ApplicationSettings")

				For Each item In dict

				DGtextBox = "DGTxtBx" + CStr(i)
				DTtextBox = "DTTxtBx" + CStr(i)
				DStextBox = "DSTxtBx" + CStr(i)

						.WriteStartElements ("code id")
						.WriteString (dict.key(i))

								.WriteStartElements ("docgroup")
								.WriteString(DGtextBox)
								.WriteEndElement()

								.WriteStartElements ("doctype")
								.WriteString(DTtextBox)
								.WriteEndElement()

								.WriteStartElements ("docsubtype")
								.WriteString(DStextBox)
								.WriteEndElement()

						.WriteEndElement()
						i = i + 1
				Next item

				.WriteEndElement()
				.WriteEndElement()
				.WriteEndDocument()
				.Close()

				MsgBox ("Did it work?")
		End With
End Sub

Open in new window


My idea is to loop through the items in the scripting dictionary (the "code id") and create the three elements in each.

*Update* Mis-labeled one two of the variables.
0
 
LVL 1

Author Comment

by:nobushi
ID: 39285394
Thanks Jason,

But the "code id" must not work like a dictionary as this value is inputted by the user. So how would it work then with the code? Please see below of my form, this information is inserted by the user.

settingsform
0
 
LVL 5

Expert Comment

by:Jason Schlueter
ID: 39285430
Try using this instead.

Set dict = New Dictionary
	With dict
		.add key:=CDTxtbx1.text.tostring(), item:=1
		.add key:=CDTxtbx2.text.tostring(), item:=2
		.add key:=CDTxtbx3.text.tostring(), item:=3
		.add key:=CDTxtbx4.text.tostring(), item:=4

Open in new window


Does the user have to put in all four codes?  If not, we can only add the ones we need.
0
Industry Leaders: 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 Comment

by:nobushi
ID: 39285446
No they dont have to put in all codes but i would like all boxes recognized in the event they want to use all 4.

Please give me some time to figure this out as I am brand new to programming with no previous experiance from before.
0
 
LVL 5

Expert Comment

by:Jason Schlueter
ID: 39285470
Welcome to programming!  I hope that these forums are helpful.

Take your time.  Not a problem.

If they can enter less than four codes then we would need to detect which ones had data in them instead of a space or a null/empty value.  We could create a simple drop down with the value 1 to 4 and they would only be given that many records to fill out.  It is kind of cheating but with a very small UI like this with so few options, I don't see a problem with it.

If you anticipate growth (more than four codes, multiple columns, other conditions), we should build this a bit differently.
0
 
LVL 1

Author Comment

by:nobushi
ID: 39285539
Thanks Jason.

We dont anticipate any growth for these. Will pass on the dropdown, all I want for right now is for the user to fill in this information hit the save button and have the values written to the xml file.
0
 
LVL 1

Author Comment

by:nobushi
ID: 39285654
Jason, im stuck.

This is what I have so far.

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim settings As New XmlWriterSettings()
        settings.Indent = True

        ' Initialize the Xmlwriter.
        Dim xmlwrt As XmlWriter = XmlWriter.Create(xmlfile, settings)

        Dim i As Integer = 1
        Dim DGtextBox As String
        Dim DTtextBox As String
        Dim DSTtextBox As String

        Dim dict As Dictionary(Of String, Integer) =
            New Dictionary(Of String, Integer)

        dict.Add(key:=CDtxtbx1.Text.ToString(), value:=1)
        dict.Add(key:=CDtxtbx2.Text.ToString(), value:=2)
        dict.Add(key:=CDtxtbx3.Text.ToString(), value:=3)
        dict.Add(key:=CDtxtbx4.Text.ToString(), value:=4)

        With xmlwrt

            .WriteStartDocument()
            .WriteComment("MyProgramSettings")
            .WriteStartElement("Settings")
            .WriteStartElement("ApplicationSettings")

            For Each item In dict

                DGtextBox = "DGTxtBx" + CStr(i)
                DTtextBox = "DTTxtBx" + CStr(i)
                DSTtextBox = "DSTxtBx" + CStr(i)

                .WriteStartElement("code id")
                .WriteString(dict.Keys(i))

                .WriteStartElement("docgroup")
                .WriteString(DGtextBox)
                .WriteEndElement()

                .WriteStartElement("doctype")
                .WriteString(DTtextBox)
                .WriteEndElement()

                .WriteStartElement("docsubtype")
                .WriteString(DSTtextBox)
                .WriteEndElement()

                .WriteEndElement()
                i = i + 1

            Next item

            .WriteEndElement()
            .WriteEndElement()
            .WriteEndDocument()
            .Close()

            MsgBox("Settings saved successfully!", MsgBoxStyle.Information, "Settings")

        End With

    End Sub

Open in new window


When i click the save button i get this error:

An unhandled exception of type 'System.ArgumentException' occurred in System.Xml.dll
Additional information: Invalid name character in 'code id'. The ' ' character, hexadecimal value 0x20, cannot be included in a name.

It bombs on this line:

.WriteStartElement("code id")

Open in new window


BTW i had to change the code a little as VStudio didnt like some of your entries.

Any ideas?
0
 
LVL 5

Expert Comment

by:Jason Schlueter
ID: 39285677
I'm not surprised that you had to make some changes as I'm writing this out from memory.  I'm sure my syntax isn't perfect! :)

Use an underscore in place of the space.  XML elements can not contain spaces.

http://www.w3schools.com/xml/xml_elements.asp

I didn't notice that.
0
 
LVL 5

Expert Comment

by:Jason Schlueter
ID: 39285716
I think that I misunderstood the intent.  In the original XML we did not have an attribute for the code but you want it to be an attribute in the new XML?

It is the difference between this:

(Inside the XML tag is an attribute.)
<code id="FTO">

and this

(Outside the tag is value/content.)
<code1>FTO</code1>


I was treating it as content.  If we want it to be an attribute we will need to call a different method from the XmlWriter.  Is there a ".WriteAttribute" or something like that?
0
 
LVL 1

Author Comment

by:nobushi
ID: 39285721
That did the trick Jason. Thanks.

Though its completely changed the format of the xml.

After hitting the save button my xml file looks like this now:

<?xml version="1.0" encoding="utf-8"?>
<!--MyProgramSettings-->
<Settings>
  <ApplicationSettings>
    <code_id>FTI<docgroup>DGTxtBx1</docgroup><doctype>DTTxtBx1</doctype><docsubtype>DSTxtBx1</docsubtype></code_id>
    <code_id>ACL<docgroup>DGTxtBx2</docgroup><doctype>DTTxtBx2</doctype><docsubtype>DSTxtBx2</docsubtype></code_id>
    <code_id>TES<docgroup>DGTxtBx3</docgroup><doctype>DTTxtBx3</doctype><docsubtype>DSTxtBx3</docsubtype></code_id>
    <code_id>
      <docgroup>DGTxtBx4</docgroup>
      <doctype>DTTxtBx4</doctype>
      <docsubtype>DSTxtBx4</docsubtype>
    </code_id>
  </ApplicationSettings>
</Settings>

Open in new window


instead of this:

<?xml version="1.0" encoding="utf-8"?>
<!--MyProgram Settings-->
<Settings>
  <ApplicationSettings>
    <code id="FTO">
      <docgroup>Operations</docgroup>
      <doctype>Funds Transfer</doctype>
      <docsubtype>Out</docsubtype>
    </code>
    <code id="FTI">
      <docgroup>null</docgroup>
      <doctype>null</doctype>
      <docsubtype>null</docsubtype>
    </code>
    <code id="ACL">
      <docgroup>Documentation</docgroup>
      <doctype>Client Documentation</doctype>
      <docsubtype>Termination</docsubtype>
    </code>
    <code id="TBA">
      <docgroup>Operations</docgroup>
      <doctype>Funds Transfer Credit</doctype>
      <docsubtype>Reversed</docsubtype>
    </code>
  </ApplicationSettings>
  </Settings>

Open in new window


Any ideas?
0
 
LVL 1

Author Comment

by:nobushi
ID: 39285730
Ahh ok i see.

Yes there is an attribute for the code.
0
 
LVL 5

Accepted Solution

by:
Jason Schlueter earned 500 total points
ID: 39285759
Wow!  Close but not yet there.

Use that attribute code to put the "FTO" inside the code tag.

The missing carriage returns are not a problem but we can add them if we need to later.

We are pulling the literal string that we built as the element content, not the item that it is referring to.

Instead of

.WriteString(DSTtextBox)


Try

.WriteString(DSTtextBox.Text.ToString())
0
 
LVL 5

Expert Comment

by:Jason Schlueter
ID: 39285770
I also noticed as I re-read my last comment that DSTtextBox is misspelled.  This is why the last chunk of XML was cleanly indented.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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