Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9204421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:42:29+00:00 2026-06-17T23:42:29+00:00

I am creating an XML with serialization and I have a problem when I

  • 0

I am creating an XML with serialization and I have a problem when I need to create attributes dynamically to an element. I am adding elements of dates that have a price data in them. My class structure for the XML looks like this:

 <XmlRoot(ElementName:="root")>
    Public Class DateXML
        <XmlElement(ElementName:="date")> _
        Public Property Dates As List(Of DatePrice)
    End Class

    Public Class DatePrice
        <XmlAttribute("value")> _
        Public Property DateValue As String
        <XmlElement("price")> _
        Public Property Price As String
    End Class

The XML will then look like this for example:

<root>
    <date value="2013-01-01">
        <price>100.00</price>
    </date>
    <date value="2013-01-02">
        <price>100.00</price>
    </date>
    <date value="2013-01-03">
        <price>100.00</price>
    </date>
    <date value="2013-01-04">
        <price>100.00</price>
    </date>
    <date value="2013-01-08">
        <price>100.00</price>
    </date>
    <date value="2013-01-12">
        <price>100.00</price>
    </date>
</root>

My problem is with a requirement for the XML. If I have multiple date values with the same price I should put them in one node using value1="2013-01-01" value2="2013-01-02" etc. Additionally if the dates follow each other I should use from="" to="" attributes and I am allowed to mix these attributes. The goal is to create as few of the date nodes as possible using attributes. So the example above should be something like this:

<root>
    <date from="2013-01-01" to="2013-01-04">
        <price>100.00</price>
    </date>
    <date value1="2013-01-08" value2="2013-01-12">
        <price>100.00</price>
    </date>
</root>

How should I create my DatePrice class to accomplish this requirement? I am getting the data per date from the database. I could add the from and to attributes as a properties to the class but is it possible to add attributes dynamically for the value1, value2…?

EDIT:

I’m a bit closer now after discovering how to serialize an dictionary. Here is what I have tried:

<XmlRoot(ElementName:="root")>
Public Class XmlObject
    <XmlElement(ElementName:="element")> _
    Public Property element As String 
    Public Property elementAttributes As ValueAttributes
End Class

Public Class ValueAttributes
    Public Property values As SerializableDictionary(Of String, String)
End Class

Dictionary class that implements IXmlSerializable:

Public Class SerializableDictionary(Of AttributeName, AttributeValue)
    Inherits Dictionary(Of String, String)
    Implements IXmlSerializable

    Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
        Return Nothing
    End Function

    Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml 
        If reader.HasAttributes Then
            While reader.MoveToNextAttribute()

                Dim key As String = reader.Name
                Dim value As String = reader.Value
                Me.Add(key, value)
            End While

            reader.MoveToElement()
        End If
    End Sub

    Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml     
        For Each kvp As KeyValuePair(Of String, String) In Me
            writer.WriteAttributeString(kvp.Key, kvp.Value)
        Next
    End Sub

End Class

Using these I’ll get an XML that looks like this:

<root>
  <element>the element value</element>
  <elementAttributes>
    <values value1="1" value2="2" value3="3" />
  </elementAttributes>
</root>

Now I just would need to somehow figure how to move the attributes to the element and not write the elementAttributes structure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:42:30+00:00Added an answer on June 17, 2026 at 11:42 pm

    Going to answer my own question as I have a solution that I think I can live with for now or at least going to give it a try to see if it will be enough. So to create the nodes and add the attributes dynamically to them I have created a class that looks like this:

    Public Class DateElement
        Implements IXmlSerializable
    
        Public Property DateValueAttributes As Dictionary(Of String, String)
        Public Property Price As String
    
        Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
            Return Nothing
        End Function
    
        Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
            If reader.HasAttributes Then
                DateValueAttributes = New Dictionary(Of String, String)
                While reader.MoveToNextAttribute()
                    DateValueAttributes.Add(reader.Name, reader.Value)
                End While
    
                reader.MoveToElement()
            End If
    
            Dim wasEmpty As Boolean = reader.IsEmptyElement
            reader.Read()
    
            If wasEmpty Then
                Return
            End If
    
            While reader.NodeType <> System.Xml.XmlNodeType.EndElement
                reader.ReadStartElement("price")
                Price = reader.ReadString()
                reader.ReadEndElement()
            End While
    
            reader.ReadEndElement()
    
        End Sub
    
        Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
            For Each kvp As KeyValuePair(Of String, String) In Me.DateValueAttributes
                writer.WriteAttributeString(kvp.Key, kvp.Value)
            Next
    
            If Price IsNot Nothing Then
                writer.WriteStartElement("price")
                writer.WriteString(Price)
                writer.WriteEndElement()
            End If
    
        End Sub
    End Class
    

    I can use this class in my xml object for example:

    <XmlRoot(ElementName:="root")>
    Public Class XmlObject
        <XmlElement("date")>
        Public Property [element] As DateElement
    End Class
    

    And then creating the object in code for example like this:

    Dim xml As New XmlObject()
            xml.element = New DateElement()
            xml.element.DateValueAttributes = New Dictionary(Of String, String)
            xml.element.DateValueAttributes.Add("value1", "2013-01-01")
            xml.element.DateValueAttributes.Add("value2", "2013-01-02")
            xml.element.DateValueAttributes.Add("value3", "2013-01-03")
            xml.element.Price = "100.00"
    

    Will give a result XML that looks like this:

    <root>
      <date value1="2013-01-01" value2="2013-01-02" value3="2013-01-03">
        <price>100.00</price>
      </date>
    </root>
    

    I think I am close enough now so I can work with this. If anyone have any ideas I would be more than happy to hear them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to create an xml file, that contains an element with attributes like:
I am creating an XML file using the System.Xml.Serialization module. I have a class
I need to create an xml file that is supposed to look like this.
I have a C# class that inherits from DynamicObject and using serialization is creating
I'm creating XML element via minidom: ele = doc.createElement(ele) main.appendChild(ele) ele.attributes['name']= bla But the
I have an problem in creating xml with the <c:condition> <a:condition> <fieldName>fieldName</fieldName> <fieldTest>fieldTest</fieldTest> <fieldValues>
I'm creating xml-like mark-up language using System.Xml.XmTextWriter that will be read by a third
I'm creating an XML file using Perl and XML::Simple module. I successfully create the
I am creating an XML document on the fly. and I need to know
I am creating an XML web service that passes an array of custom types.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.