日期:2014-05-19  浏览次数:20966 次

[求助]关于C#中Dataset操作XML的问题
求教高手如何使用C#中的DataSet生成如下内容的XML文件,要求格式完全相同且要使用DataSet手动生成,主要是不知如何将dt2节点作为dt1节点的子节点:
test.xml:
<?xml   version= "1.0 "?>
<testds>
    <dt1   name= "dt1 ">
        <id> a </id>
        <dt1c2> 111 </dt1c2>
        <dt2   name= "dt2 ">
              <dt2c1> d2 </dt2c1>
              <dt2c2> adad </dt2c2>
        </dt2>
    </dt1>
   
</testds>



------解决方案--------------------
将 dt1 和 dt2 建立 Relations, 并将其内嵌显示设置为 True

Dim objConn As OracleConnection
Dim objDataAdapter As OracleDataAdapter
Dim strConn As String = "user id=scott;password=tiger;data source=orcl "
Dim strSql As String
Dim ds As DataSet
Dim objXmlDataDoc As XmlDataDocument
Try
objConn = New OracleConnection(strConn)
ds = New DataSet( "DeptEmp ")

strSql = "select * from emp "
objDataAdapter = New OracleDataAdapter(strSql, objConn)
objDataAdapter.Fill(ds, "Employee ")

strSql = "select * from dept "
objDataAdapter.SelectCommand.CommandText = strSql
objDataAdapter.Fill(ds, "Dept ")

ds.CaseSensitive = False
ds.Relations.Add( "DeptEmployees ", _
ds.Tables( "Dept ").Columns( "DeptNo "), _
ds.Tables( "Employee ").Columns( "DeptNo ")).Nested = True

ds.WriteXml( "DeptEmployees.xml ")

Catch ex As Exception
Console.WriteLine(ex.Message)
Console.ReadLine()
End Try