Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

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

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
nobushi
Asked:
nobushi
  • 7
  • 6
1 Solution
 
Jason SchlueterIT ManagerCommented:
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
 
nobushiAuthor Commented:
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
 
Jason SchlueterIT ManagerCommented:
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!

 
nobushiAuthor Commented:
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
 
Jason SchlueterIT ManagerCommented:
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
 
nobushiAuthor Commented:
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
 
nobushiAuthor Commented:
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
 
Jason SchlueterIT ManagerCommented:
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
 
Jason SchlueterIT ManagerCommented:
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
 
nobushiAuthor Commented:
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
 
nobushiAuthor Commented:
Ahh ok i see.

Yes there is an attribute for the code.
0
 
Jason SchlueterIT ManagerCommented:
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
 
Jason SchlueterIT ManagerCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now