0

I cannot create multiple VB.NET XML elements with same name in the same level under configuration element. I'm only able to create one attribute with a name of "Description1". What exactly am I not understanding?

  <configuration name="Default" quantity="1">
    <attribute name="Description1" value="1" />
    <attribute name="Description2" value="1" />
  </configuration>

I have tried to change attribute element to array, but I guess I have no idea what I'm doing.

Public Class transaction
    <Xml.Serialization.XmlElement>
    Public Property document() As document

    <Xml.Serialization.XmlAttribute>
    Public Property type() As String
    <Xml.Serialization.XmlAttribute>
    Public Property [date]() As Integer
    <Xml.Serialization.XmlAttribute>
    Public Property name() As String

End Class

Public Class document
    <Xml.Serialization.XmlAttribute>
    Public Property aliasset As String
    <Xml.Serialization.XmlAttribute>
    Public Property eid As String

    <Xml.Serialization.XmlElement>
    Public Property configuration As Configuration
End Class

Public Class configuration
    <Xml.Serialization.XmlAttribute>
    Public Property name As String
    <Xml.Serialization.XmlAttribute>
    Public Property quantity As Integer
    <Xml.Serialization.XmlElement>
    Public Property attribute As attribute
End Class

Public Class attribute
    <Xml.Serialization.XmlAttribute>
    Public Property name As String
    <Xml.Serialization.XmlAttribute>
    Public Property value As String
End Class

I'm trying to use this with this:

Private Sub Create_button_Click(sender As Object, e As EventArgs) Handles Create_button.Click

    Dim cf As New Transactions()
    cf.transaction = New transaction() With {.type = "wf_export_document_attributes", .date = 1235456, .name = "QUALITY"}
    cf.transaction.document = New document() With {.aliasset = "", .eid = "1234567"}
    cf.transaction.document.configuration = New configuration() With {.name = "Default", .quantity = "1"}
    cf.transaction.document.configuration.attribute = New attribute() With {.name = "Description", .value = "1"}
    cf.transaction.document.configuration.attribute = New attribute() With {.name = "Description2", .value = "1"}

    Dim s As New System.Xml.Serialization.XmlSerializer(GetType(Transactions))

    Using fs As New System.IO.FileStream("C:\temp\file.xml", System.IO.FileMode.OpenOrCreate)
        s.Serialize(fs, cf)
    End Using
End Sub
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Ungtskans
  • 3
  • 2
  • You should use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) - I can see one place where the types don't match: `.quantity = "1"` but `.quantity` is declared as Integer. – Andrew Morton Jul 17 '19 at 10:56

1 Answers1

0

You need to have some type of variable which can hold multiple attributes, such as a List(Of Attribute), like this:

Public Class configuration
    <Xml.Serialization.XmlAttribute>
    Public Property name As String

    <Xml.Serialization.XmlAttribute>
    Public Property quantity As Integer

    <Xml.Serialization.XmlElement(ElementName:="attribute")>
    Public Property attributes As List(Of attribute)

End Class

And then to add them:

Dim cf As New Transactions()
cf.transaction = New transaction() With {.type = "wf_export_document_attributes", .date = 1235456, .name = "QUALITY"}
cf.transaction.document = New document() With {.aliasset = "", .eid = "1234567"}
cf.transaction.document.configuration = New configuration() With {.name = "Default", .quantity = 1}
cf.transaction.document.configuration.attributes = New List(Of attribute) From {New attribute With {.name = "Description1", .value = "1"}}
cf.transaction.document.configuration.attributes.Add(New attribute() With {.name = "Description1", .value = "1"})

Dim s As New System.Xml.Serialization.XmlSerializer(GetType(Transactions))

Using fs As New System.IO.FileStream("C:\temp\file.xml", System.IO.FileMode.Create)
    s.Serialize(fs, cf)
End Using

Which resulted in this:

<?xml version="1.0"?>
<Transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <transaction type="wf_export_document_attributes" date="1235456" name="QUALITY">
    <document aliasset="" eid="1234567">
      <configuration name="Default" quantity="1">
        <attribute name="Description1" value="1" />
        <attribute name="Description1" value="1" />
      </configuration>
    </document>
  </transaction>
</Transactions>

N.B. it put an extra "s>" at the end when I specified ElementName:="attribute". I don't know why. You could just name that property "attribute" instead of "attributes" if it does the same thing wrong for you. Silly me, I didn't spot the FileMode.OpenOrCreate was messing it up.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Alright this did help me a ton and got it working! I was messing with that FileMode.OpenorCreate also a while. Have to delete the XML before trying to write it again or change it to create it instead open. – Ungtskans Jul 19 '19 at 03:30