数据库中取出表:
DataTable
          col1 col2 col3
          CHINA ShangHai SH1
          CHINA ShangHai SH2
          CHINA ShangHai SH3
          CHINA Beijing BJ1
          USA Chicago CC1
          USA Chicago CC2
          USA NewYork NY1
字段分别从左到右以次排序过。
我想得到一个XML格式的字符串。 
 格式大致为:
       <col1 value="CHINA">
           <col2 value="ShangHai">
                <col3 value="SH1" />
                <col3 value="SH2" />
                <col3 value="SH3" />
           </col2>
           <col2 value="BeiJing">
                <col3 value="BJ1" />
           </col2>
        <col1 value="USA">
           <col2 value="Chicago">
                :
                :
请注意, 要求:column的个数不限,就是左边列是右列的父级。 

解决方案 »

  1.   

        Private Function GetXMLData(ByVal dt As DataTable) As String
            Dim strCol1 As String = ""
            Dim strCol2 As String = ""
            Dim strXML As String = ""        For i As Integer = 0 To dt.Rows.Count - 1
                If dt.Rows(i)("col1") <> strCol1 Then
                    If strCol1 <> "" Then
                        strXML &= Space(4) & "</col2>" & vbCrLf
                        strXML &= "</col1>" & vbCrLf
                        strCol2 = ""
                    End If
                    strCol1 = dt.Rows(i)("col1")
                    strXML &= "<col1 value=""" & strCol1 & """>" & vbCrLf
                End If
                If dt.Rows(i)("col2") <> strCol2 Then
                    If strCol2 <> "" Then
                        strXML &= Space(4) & "</col2>" & vbCrLf
                    End If
                    strCol2 = dt.Rows(i)("col2")
                    strXML &= Space(4) & "<col2 value=""" & strCol2 & """>" & vbCrLf
                End If
                strXML &= Space(8) & "<col3 value=""" & dt.Rows(i)("col3") & """ />" & vbCrLf
            Next
            strXML &= Space(4) & "</col2>" & vbCrLf
            strXML &= "</col1>" & vbCrLf        Return strXML
        End Function
    VB.net写的,转成C#就可以用了,要求数据是顺序的(通过数据库输出时ORDER BY或DataTable排序)。
      

  2.   

    private string GetXMLData(DataTable dt) 

        string strCol1 = ""; 
        string strCol2 = ""; 
        string strXML = ""; 
        
        for (int i = 0; i <= dt.Rows.Count - 1; i++) { 
            if (dt.Rows(i)("col1") != strCol1) { 
                if (!string.IsNullOrEmpty(strCol1)) { 
                    strXML += Strings.Space(4) + "</col2>" + Constants.vbCrLf; 
                    strXML += "</col1>" + Constants.vbCrLf; 
                    strCol2 = ""; 
                } 
                strCol1 = dt.Rows(i)("col1"); 
                strXML += "<col1 value=\"" + strCol1 + "\">" + Constants.vbCrLf; 
            } 
            if (dt.Rows(i)("col2") != strCol2) { 
                if (!string.IsNullOrEmpty(strCol2)) { 
                    strXML += Strings.Space(4) + "</col2>" + Constants.vbCrLf; 
                } 
                strCol2 = dt.Rows(i)("col2"); 
                strXML += Strings.Space(4) + "<col2 value=\"" + strCol2 + "\">" + Constants.vbCrLf; 
            } 
            strXML += Strings.Space(8) + "<col3 value=\"" + dt.Rows(i)("col3") + "\" />" + Constants.vbCrLf; 
        } 
        strXML += Strings.Space(4) + "</col2>" + Constants.vbCrLf; 
        strXML += "</col1>" + Constants.vbCrLf; 
        
        return strXML; 
    }
      

  3.   

    楼主可以先用SelectDistinct的方法取得Distinct(col1)
    http://hi.baidu.com/raymond_gjy/blog/item/dd04268362958998f603a6d7.html
    希望对你有帮助。
      

  4.   

    感谢 两位。 不过如果要是不定列数,你们的做法是不可行的。即,当col4,col5出现时。。
      

  5.   

    我用VB.net写的,这个没问题,C#是工具转的,可能会有问题,稍微改改就行了。
      

  6.   

        Private Function GetXMLData(ByVal dt As DataTable) As String
            Dim strCols(dt.Columns.Count - 1, 1) As String
            Dim strXML As String = ""        For i As Integer = 0 To dt.Columns.Count - 1
                strCols(i, 0) = dt.Columns(i).ColumnName
            Next
            For i As Integer = 0 To dt.Rows.Count - 1
                For j As Integer = 0 To dt.Columns.Count - 2
                    If dt.Rows(i)(j) <> strCols(j, 1) Then
                        If strCols(j, 1) <> "" Then
                            For k As Integer = dt.Columns.Count - 2 To j + 1 Step -1
                                strCols(k, 1) = ""
                                strXML &= Space(k * 4) & "</" & strCols(k, 0) & ">" & vbCrLf
                            Next
                            strXML &= Space(j * 4) & "</" & strCols(j, 0) & ">" & vbCrLf
                        End If
                        strCols(j, 1) = dt.Rows(i)(j)
                        strXML &= Space(j * 4) & "<" & strCols(j, 0) & " value=""" & strCols(j, 1) & """>" & vbCrLf
                    End If
                Next
                strXML &= Space(((dt.Columns.Count - 1) * 4)) & "<" & strCols(dt.Columns.Count - 1, 0) & " value=""" & dt.Rows(i)(dt.Columns.Count - 1) & """ />" & vbCrLf
            Next
            For k As Integer = dt.Columns.Count - 2 To 0 Step -1
                strXML &= Space(k * 4) & "</" & strCols(k, 0) & ">" & vbCrLf
            Next        Return strXML
        End Function我试了四列没问题。
      

  7.   

    这帖子害人不浅啊。。大家不要学习。要学习就学习System.Xml和