我是xml和.net的初学者。我有一系列的复选框列表,我想根据用户选择生成一个xml文件。我希望xml文件是这样的。
<?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>
但我只能得到这个
<?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>
这是我用来创建xml文件的VB代码。请告诉我我错过了什么。非常感谢你的帮助。
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 & " " & "<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
答案 0 :(得分:0)
您需要将类别元素创建移出循环:
w.WriteStartElement("CATEGORY")
w.WriteAttributeString("Name", "Year")
For Each itemacademicyear In cblacademicyear.Items
If itemacademicyear.Selected = True Then
lblselected.Text = lblselected.Text & itemacademicyear.Text & " " & "<a href='#'>remove</a>" & "<BR>"
'xml bit
w.WriteStartElement("SELECTED")
w.WriteAttributeString("Value", itemacademicyear.Text)
w.WriteEndElement()
End If
Next
w.WriteEndElement()