Connecting Tech Pros Worldwide Help | Site Map

looping Datarows with XMLTEXTWRITER

Mickey N
Guest
 
Posts: n/a
#1: May 10 '06
Hi,
I am trying to create an XML file from a SQL view that I have in my
database. The code woeks fine but I have a problem trying to define an
atribute. For example
PO number 123 contains 2 lines I wish to write it to the XML file as
<PONUM>1
<POLINE>1
<ITEM>Apple</ITEM>
</POLINE>
<POLINE>2
<ITEM>PEAR</ITEM>
</POLINE>
</PONUM>

I get this at the moment because I can not loop the PO line part

<PONUM>1
<POLINE>1
<ITEM>Apple</ITEM>
</POLINE>
</PONUM>
<PONUM>1
<POLINE>2
<ITEM>PEAR</ITEM>
</POLINE>
</PONUM>

I would like to loop this bit in the code if possible checking the PONUM
before to see it needs to loop again.
writer.WriteStartElement("POLINE")
writer.WriteString(drCurrent("d_ORDER_LINE"))
writer.WriteEndElement()

any suggestion greatly appreciated


My code is as follows:

sub Main()
Dim sConnectionString As String
sConnectionString = "Password=sa;User ID=sa;" & _
"Initial Catalog=ncs41data;" & _
"Data Source=LATITUDE0507"

Dim objConn As New SqlConnection(sConnectionString)
objConn.Open()

Dim daNcs41data As _
New SqlDataAdapter("select * from ecp_order_view", objConn)

Dim dsNcs41data As New DataSet("Ncs41data")

daNcs41data.FillSchema(dsNcs41data, SchemaType.Source,
"ecp_order_view")
daNcs41data.Fill(dsNcs41data, "ecp_order_view")

Dim tblNcs41data As DataTable
tblNcs41data = dsNcs41data.Tables("ecp_order_view")

Dim writer As XmlTextWriter = Nothing
writer = New
XmlTextWriter(System.IO.File.CreateText("C:\temp\x _ecp.xml")) 'this writes
the results to file 1.txt

Dim drCurrent As DataRow '

For Each drCurrent In tblNcs41data.Rows

writer.WriteStartElement("PONUM")
writer.WriteString(drCurrent("h_ORDER_ID"))
writer.WriteStartElement("POLINE")
writer.WriteString(drCurrent("d_ORDER_LINE"))
writer.WriteEndElement()
writer.WriteEndElement()
NEXT
end sub

thank you
Closed Thread