XML : How to control two Data Table values to write XML vb.net

There is a result of SQL query where I get paperBoy data along with payments done to the guy (cash,check or both cash and check)

DataTable looks like:

  ID Name    Payment  cashInfo1  cashInfo2  CheckInfo1  CheckInfo2  1  Rivera  Cash     xx         xx         null        null     1  Rivera  Check    null       null       dr          o1      1  Rivera  Both     xx         yy         rr          rr      2  Gomez   Cash     xx         xx         null        null     2  Gomez   Cash     xx         xx         null        null        2  Gomez   Both     xx         yy         rr          rr    

I want an XML based on that like

  <All>     <PaperBoy>        <Name>Rivera</Name>        <CashPayment>             <cashInfo1>xx</cashInfo1>            <cashInfo2>xx</cashInfo2>        </CashPayment>        <CheckPayment>             <CheckInfo1>dr</CheckInfo1>            <CheckInfo2>o1</CheckInfo2>        </CheckPayment>        <Both>            <CashPayment>                <cashInfo1>xx</cashInfo1>               <cashInfo2>yy</cashInfo2>           </CashPayment>           <CheckPayment>                <CheckInfo1>rr</CheckInfo1>               <CheckInfo2>rr</CheckInfo2>           </CheckPayment>        </Both>     </PaperBoy>     <PaperBoy>        <Name>Gomez</Name>        <CashPayment>             <cashInfo1>xx</cashInfo1>            <cashInfo2>xx</cashInfo2>        </CashPayment>        <CashPayment>             <cashInfo1>xx</cashInfo1>            <cashInfo2>xx</cashInfo2>        </CashPayment>                  <Both>            <CashPayment>                <cashInfo1>xx</cashInfo1>               <cashInfo2>yy</cashInfo2>           </CashPayment>           <CheckPayment>                <CheckInfo1>rr</CheckInfo1>               <CheckInfo2>rr</CheckInfo2>           </CheckPayment>        </Both>     </PaperBoy>  </All>    

to do so I created a method to open and close Root and other to handle the paperBoy logic

  Public Sub myMethod(ByVal XmlDocumentPath As String)          Try              Dim XmlWriter As New Xml.XmlTextWriter(XmlDocumentPath, System.Text.Encoding.GetEncoding("utf-8"))              XmlWriter.Formatting = Xml.Formatting.Indented               XmlWriter.WriteRaw("<?xml version=""1.0"" encoding=""utf-8"" ?>")              XmlWriter.WriteStartElement("All")              handlePaperBoy(XmlWriter)              XmlWriter.WriteFullEndElement() '</All>              XmlWriter.Close()      Catch ex As Exception              MsgBox( ex.Message)          End Try      End Sub          Private Sub handlePaperBoy(ByRef XmlWriter As XmlTextWriter)        Dim dT As New Data.DataTable 'supposing I filled it already      Dim currentId = "-1"      For Each DR As DataRow In dT.Rows          If currentId <> DR.Item("ID") Then              currentId = DR.Item("ID")              XmlWriter.WriteStartElement("PaperBoy")              XmlWriter.WriteElementString("Name", DR.Item("Name"))              Select Case DR.Item("Payment")                  Case "Cash"                       'add xml elements                  Case "Check"                       'add xml elements                  Case "Both"                       'add xml elements              End Select              XmlWriter.WriteEndElement() '</PaperBoy>          End If      Next   End Sub    

However I got stuck at that point because to add Paperboy tags I use a flag to indicate if ID changed, but How could I control the Payment logic?

I think I would need to save Paperboy Data and then keep on looping until ID changed then fill corresponding tags.

What could I do to solve the issue?

No comments:

Post a Comment