【问题标题】:How to control two Data Table values to write XML vb.net如何控制两个数据表值写入 XML vb.net
【发布时间】:2016-03-24 20:05:08
【问题描述】:

SQL query 的结果是我得到 paperBoy 数据以及支付给 (cash,check or both cash and check) 的人的付款

数据表看起来像:

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

我想要一个基于类似的 XML

<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>

为此,我创建了一个方法来打开和关闭 Root 以及其他处理 paperBoy 逻辑的方法

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 标签,我使用标志来指示 ID 是否更改,但我如何控制 Payment 逻辑?

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

我能做些什么来解决这个问题?

【问题讨论】:

  • 所以如果下一个 id 相同,支付标签将不会被写入。是这个问题吗?
  • 没错,问题是&lt;PaperBoy&gt;的打开和关闭标签以及所有内部xml标签..

标签: xml vb.net datatable


【解决方案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

希望这会有所帮助。

【讨论】:

  • 你退房,如果需要任何帮助,请回复。
  • 好的,我得到了代码,解决问题的好方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
相关资源
最近更新 更多