1

I am a beginner developer with xml and .net. I have series of checkboxlists and I want to produce an xml file depending on the user select. I want xml file to be like this.

<?xml version="1.0" encoding="utf-8"?>
  <FILTER xmlns:x="urn:1">
    <CATEGORY Name="Year">
      <SELECTED Value="2011/2010" />
      <SELECTED Value="2010/2009" />
      <SELECTED Value="2009/2008" />
    </CATEGORY>
    <CATEGORY Name="Grade">
      <SELECTED Value="Kindergarten 1" />
    </CATEGORY>   
  </FILTER>

But I only get this

<?xml version="1.0" encoding="utf-8"?>
  <FILTER xmlns:x="urn:1">
    <CATEGORY Name="Year">
      <SELECTED Value="2011/2010" />
    </CATEGORY>
    <CATEGORY Name="Year">
      <SELECTED Value="2010/2009" />
    </CATEGORY>
    <CATEGORY Name="Year">
      <SELECTED Value="2009/2008" />
    </CATEGORY>
    <CATEGORY Name="Grade">
      <SELECTED Value="Kindergarten 1" />
    </CATEGORY>
  </FILTER>

This is the VB code i use to create xml file. Please tell me what i am missing. thanks so much for help.

Dim itemacademicyear As ListItem
Dim itemgrade As ListItem

Dim w As New XmlTextWriter(Server.MapPath("items.xml"), Encoding.UTF8)
w.Formatting = Formatting.Indented
w.WriteStartDocument()
w.WriteStartElement("FILTER")
w.WriteAttributeString("xmlns", "x", Nothing, "urn:1")

For Each itemacademicyear In cblacademicyear.Items
  If itemacademicyear.Selected = True Then
    lblselected.Text = lblselected.Text & itemacademicyear.Text & "&nbsp;" & "<a href='#'>remove</a>" & "<BR>"

    'xml bit
    w.WriteStartElement("CATEGORY")
    w.WriteAttributeString("Name", "Year")
    w.WriteStartElement("SELECTED")
    w.WriteAttributeString("Value", itemacademicyear.Text)
    w.WriteEndElement()
    w.WriteEndElement()
  End If
Next
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Laurence
  • 7,633
  • 21
  • 78
  • 129

1 Answers1

0

You need to move your category element creation out of the loop:

w.WriteStartElement("CATEGORY")
w.WriteAttributeString("Name", "Year")

For Each itemacademicyear In cblacademicyear.Items
    If itemacademicyear.Selected = True Then
        lblselected.Text = lblselected.Text & itemacademicyear.Text & "&nbsp;" & "<a href='#'>remove</a>" & "<BR>"

        'xml bit
        w.WriteStartElement("SELECTED")
        w.WriteAttributeString("Value", itemacademicyear.Text)
        w.WriteEndElement()

    End If
Next

w.WriteEndElement()
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • Thanks so much .. Mr Competent_tech .. i think i got it made :D – Laurence Nov 22 '11 at 09:34
  • That's great news! Since it did answer your question, can you click on the checkmark next to the answer to let future visitors to this question know that it resolved your issue? Thanks – competent_tech Nov 22 '11 at 21:18