如何控制两个数据表值来编写XML vb.net

时间:2015-12-18 07:02:12

标签: xml vb.net datatable

结果为new String(plainText, 0, ptLength, "UTF-8") ,我获得了paperBoy数据以及对该人protected void Button1_Click(object sender, EventArgs e) { GridView1.Visible = true; DataTable dt = new DataTable(); DataRow row = dt.NewRow(); for (int i = 0; i < GridView1.Rows.Count; i++) { TextBox txtUsrId=(TextBox)GridView1.Rows[i].FindControl("Your textbox id"); string UserID = txtUsrId.Text; string q="insert into details (name) values('"+UserID+"')"; SqlCommand cmd = new SqlCommand(q, con); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); } } 付款

DataTable看起来像:

SQL query

我想要一个基于

的XML
(cash,check or both cash and check)

这样做我创建了一个方法来打开和关闭Root以及其他来处理paperBoy逻辑

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

但是我在这一点上陷入困​​境,因为要添加<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> 标签,我使用一个标志来指示ID是否已更改,但我如何控制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 逻辑?

我想我需要保存Paperboy数据,然后继续循环,直到Payment更改,然后填写相应的代码。

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

尝试使用此代码,

Dim dT As New Data.DataTable 'supposing I filled it already
Dim currentId = "-1"
Dim intcount as integer=0
 For Each DR As DataRow In dT.Rows
          If currentId <> DR.Item("ID") Then
            if intcount<>0    'dont end tag for first record
              XmlWriter.WriteEndElement() '</PaperBoy>
            end if
            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
       else 
            Select Case DR.Item("Payment")
                Case "Cash"
                     'add xml elements
                Case "Check"
                     'add xml elements
                Case "Both"
                     'add xml elements
            End Select
         End If

         if intcount=dT.Rows.count-1 ' end tag for last record
           XmlWriter.WriteEndElement()
         end if

         intcount+=1
    Next

希望这会有所帮助。