Solved

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

Posted on 2013-06-28
13
276 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
  • 7
  • 6
13 Comments
 
LVL 5

Expert Comment

by:Jason Schlueter
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:nobushi
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:nobushi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ahh ok i see.

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

Accepted Solution

by:
Jason Schlueter earned 500 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now