Frage

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
War es hilfreich?

Lösung

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()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top