vbnet Create XML with XElement

Using connection As New SqlConnection(My.Settings.ConnectionString)
	connection.Open()

	Dim dr As SqlDataReader
	Dim cmd As SqlCommand = New SqlCommand

	With cmd
		.CommandText = "StoredProcedureName"
		.CommandType = CommandType.StoredProcedure
		.Connection = connection
		dr = .ExecuteReader
	End With
	
	Dim Items As XElement = <Items></Items>

	While dr.Read
		Dim Item = <Item>
					  <Id><%= dr("ID").ToString %></Id>
					  <Name><%= dr("Name").ToString %></Name>
					  <SubElement>
						  <Title><%= dr("Title") %></Title>
						  <Other><%= dr("Other") %></Other>
					  </SubElement>
				  </Item>

		'append other stuff (could come from another hit to DB or another class entirely)
		Dim ItemMeta = <ItemMeta>
						   <Stuff1>0</Stuff1>
						   <Stuff2>0</v>
					   </ItemMeta>
		Item.Add(ItemMeta)

		Items.Add(Item)
	End While

	dr.Close()
	connection.Close()
	connection.Dispose()
End Using

Items.SetAttributeValue(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema")
Items.SetAttributeValue(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance")

Dim XmlHeader As String = "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCrLf
XmlHeader += Items.ToString()
Creates an XML structure using data from a database.

Updated: Saturday 9th October 2010, 15:11pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.