如何使用数据集在 Visual Basic/C# 中读取和写入同一个 XML 文件?

我有一个列表,它可以包含任何对象(例如,在我下面复制的实例中,列表可以有一个“订单”或十个“订单”)。我使用了数据合同并得到了这个输出(OrderList 中只有一个订单/一个对象):


  <?xml version="1.0"?>

 -<frmStartup.SerializeClass xmlns:i="http://www.w3.org/2001/XMLSchema- 

instance" xmlns="http://schemas.datacontract.org/2004/07/BAKFAA">

 -<Orderlist>

    -<Order>

       <Number>1</Number>

       <OrderDistance>150</OrderDistance>

       -<OffsetX xmlns:a="http://schemas.microsoft.com/Arrays">

          <a:double>59.00</a:double>

          <a:double>70.80</a:double>

        </OffsetX>

       -<OffsetY xmlns:a="http://schemas.microsoft.com/2003">

          <a:double>43</a:double>

          <a:double>54</a:double>

        </OffsetY>

        <deflection>

          <a:double>76</a:double>

          <a:double>23</a:double>

        </deflection>

        <Comment> 2014 10 08 15 19 50</Comment>

     </Order>

  </Orderlist>

</frmStartup.SerializeClass>

根据我对Data Contract的理解,它返回程序中已经使用过的数据结构。


我被要求创建 XML 输出的新格式(与数据协定创建的格式不同)。在新任务中,我被要求创建一个与数据契约提供的非常不同的暂定结构。我正在做一个集成项目,这意味着 3 个不同的程序可以在同一个文件上读写。因此,在输出文件中,还有其他变量在当前程序中甚至不可用。输出文件需要类似于这个(与上面数据契约创建的输出比较):


  <Country>

    <Company>

       <CompanyName>aa</CompanyName>

          <branch>

              <branchName>bb</branchName>

                <OrderList>

                   <order>

                     <comment>32 53 65 76 854</comment>

                     <OrderDistance>532</OrderDistance>

                       <deflection>

                         <offsetX>0</offsetX>

                         <offsetY>0</offsetY>

                         <deflValue>10.45</deflValue>

                       </deflection>

                   </deflection>

              </order>

             </OrderList>

          </branch> 

     </Company>

  </Country>                   

我尝试使用数据集并将值复制到不同的表中,最后从 DataSet 创建 XML。使用 DataSet 的好处是我可以复制其中的 XML 输入文件,只更改程序过程中更改的值,并将这些更改与那些未更改的变量一起保存在输出文件中。这对我来说似乎是一种非常困难的方法,我必须跟踪主键和外键(如果我错了,请纠正我)。请帮助我找到解决此问题的更好方法。


慕运维8079593
浏览 188回答 1
1回答

HUX布斯

我能够自己解决问题。我上传了一个所需的输出模板,在程序的根目录中只有一个对象,如下所示:&nbsp;<Country>&nbsp; &nbsp; <Company>&nbsp; &nbsp; &nbsp; &nbsp;<CompanyName>aa</CompanyName>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <branch>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <branchName>bb</branchName>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <OrderList>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<order>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<comment>32 53 65 76 854</comment>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<OrderDistance>532</OrderDistance>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<deflection>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<offsetX>0</offsetX>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<offsetY>0</offsetY>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<deflValue>10.45</deflValue>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</deflection>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<deflection>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<offsetX>0</offsetX>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<offsetY>0</offsetY>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<deflValue>10.45</deflValue>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</deflection>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </order>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</OrderList>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </branch>&nbsp;&nbsp; &nbsp; &nbsp;</Company>&nbsp; </Country>&nbsp; &nbsp;然后我将上传的 XML 文件读取到数据集,然后从程序中计算该程序创建的对象的数量,并循环遍历它们以复制我想要的数据集表中的值。我添加了新行以添加需要信息(列)。为了跟踪表之间的关系,我使用了如下主键和外键:Public Shared XMLDom As DataSet = New DataSetXMLDom.ReadXml("../../XMLOutput_template.xml")&nbsp;For k = 0 To orderlistInXMl.Count - 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XMLDom.Tables("order").Rows.Add()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XMLDom.Tables("order").Rows(k).Item("orderNumber") =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;orderlistInXMl.Item(k).DropNumber&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XMLDom.Tables("order").Rows(k).Item("OrderDistance") =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;basinlistInXMl.Item(k).Distance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XMLDom.Tables("order").Rows(k).Item("orderlist_id") =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XMLDom.Tables("orderlist").Rows(0).Item("orderlist_id")解决此问题的关键是在程序的根目录中上传所需解决方案的模板。通过这种方式,我不必创建表关系,第二个是上面代码中的最后一个命令,它使用表中父项的主键检查子项的外键。
打开App,查看更多内容
随时随地看视频慕课网APP