如何填充具有XML架构的DataSet

时间:2016-01-14 07:04:38

标签: c# xml dataset

这是针对大学的,所以我不希望有人为我做这件事,只需要一些帮助就可以了解哪条路。

无论如何,我需要将数据库中的信息导出到XML文件中,遵守XML Schema提供的规则。我使用的解决方案不起作用,我不知道是不是因为这是错误的解决方案,或者我错过了一步。

基本上我创建了一个DataSet并将我的XML Schema加载到其中。 然后我从DataSet获取DataTableCollection,并用相应的数据填充每个DataTable。 我遇到的问题是我之后创建的XML文件不符合关系规则。

代码如下:

/*
  there's code here to make the query to the database
  basically, it's selecting a stored procedure that returns several 
  inner joined tables, with different columns data that I'll need to put
  in the XML file
*/   

SqlDataReader dr = cmd.ExecuteReader();

/*
   there's code here to make sure that there's data
*/                    
DataSet dataSet = new DataSet();
dataSet.ReadXmlSchema("XML_Schema.xsd");            
FillTables(dr, dataSet.Tables);
string xml = ds.GetXml();

至于我的FillTables代码,我只是从我的DataTableCollection中获取每个DataTable,并且每个DataTable都添加了我的SqlDataReader中包含的相应数据。一个例子就是这个:

dt.Rows.Add(new object[] { dr["tt_id"],dr["cod"],dr["ticketState"],dr["ticketDescription"]});

毕竟,我有一个XML文件,如下所示:

<?xml version="1.0"?>
<NewDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ticket status="Closed" ticketId="aaa" type="LF1010">
    <description>...</description>
  </ticket>
  <owner email="1" name="zarolho" ownerId="uEmail1">uEmail1</owner>
  <supervisor email="aEmail2" name="pp" technicianID="2">2</supervisor>
  <type_type name="Login Failed" typeId="LF1010">LF1010</type_type>
 <actions/>
 <action endDate="14-01-2016 02:31:52" beginDate="14-01-2016 02:31:52" orderNum="1">1</action>
 <action endDate="14-01-2016 02:31:52" beginDate="14-01-2016 02:31:52" orderNum="2">2</action>
</NewDataSet>

如果它看起来像这样,基于Visual Studio生成的XML示例:

<?xml version="1.0" encoding="utf-8"?>
<ticket type="type1" ticketId="ticketId1" status="status1">
  <owner ownerId="ownerId1" name="name1" email="email1">owner1</owner>
  <supervisor technicianID="technicianID1" name="name1" email="email1">supervisor1</supervisor>
  <description>description1</description>
  <type_type typeId="typeId1" name="name1">type_type1</type_type>
  <actions>
    <action orderNum="orderNum1" beginDate="beginDate1" endDate="endDate1">action1</action>
    <action orderNum="orderNum2" beginDate="beginDate2" endDate="endDate2">action2</action>
    <action orderNum="orderNum3" beginDate="beginDate3" endDate="endDate3">action3</action>
  </actions>
</ticket>

问题似乎是我的DataSet不尊重表关系,因为我没有正确填充它们...我尝试过使用SqlDataAdapters,但它也无法正常工作。

我的SqlDataAdapters代码如下:

string query_ticket = "select cod as ticketId, ticketState as status, ticketDescription as description, ticketType as type from vi_Ticket where cod=@cod";
 SqlDataAdapter adapter = new SqlDataAdapter(query_ticket, con);
 adapter.SelectCommand.Parameters.Add(cod);
 adapter.Fill(ds,"ticket");

 string query_owner = "SELECT anumber as technicianID, name, email FROM dbo.Technician where anumber=2";
  SqlDataAdapter adapter2 = new SqlDataAdapter(query_owner, con);
  adapter2.Fill(ds, "supervisor");

但它给了我一个ConstraintException

  

无法启用约束。一行或多行包含违反&gt;非空,唯一或外键约束的值。

所以我不确定哪种方法可以继续尝试找到解决问题的正确方法。

很抱歉,如果我的问题没有遵循每条规则,如果你指出错误,我会尝试修复它们或改进我的问题。

1 个答案:

答案 0 :(得分:0)

正如T凯利所说,问题在于,当我填写DataTables时,我并没有建立表格之间的关系。

解决方案非常简单,我只需要为父级分配一个id值,每个孩子也必须使用该ID。

For example, being tickets the parent table

dt.Rows.Add(new object[] { dr["tt_id"],dr["cod"],dr["ticketState"],dr["ticketDescription"],id});

and for a child of tickets, like owners, I would have to use that same id in the end, so that the relationship was estabilished.

感谢帮助人员。

相关问题