分层XML到一个平面DataTable C#

时间:2014-06-29 16:15:35

标签: c# .net xml dataset

我试图找出将分层xml文件展平为展平数据集表的最佳方法。我是用C#编写的,并且更喜欢使用内置且优雅的.net功能。对不起格式化 - 不知道为什么它变得如此奇怪。

var dataset = new DataSet();
dataset.ReadXml(@"xmlfile.xml"); //reading in the hierarchical xml
//do something here
data.WriteXml(@"flatdatasetfile.xml"); //write out the flat dataset here

这是我的xml:

<xml>
<customers>
 <customer_details>
  <customer>1</customer>
  <name>Smith</name>
 </customer_details>
    <items>
     <item_details>
      <item>1</item>
      <description>blah</description>
     </item_details>
     <item_details>
      <item>2</item>
      <description>blah2</description>
     </item_details>
    </items>
 <customer_details>
  <customer>2</customer>
  <name>Jones</name>
 </customer_details>
    <items>
     <item_details>
      <item>1</item>
      <description>blah</description>
     </item_details>
     <item_details>
      <item>3</item>
      <description>blah3</description>
     </item_details>
    </items>
</customers>
</xml>

预期结果:

<dataset>
  <customer_item>
    <customer>1</customer>
    <name>Smith</name>
    <item>1</item>
    <description>blah</description>
  </customer_item>
  <customer_item>
    <customer>1</customer>
    <name>Smith</name>
    <item>2</item>
    <description>blah2</description>
  </customer_item>
  <customer_item>
    <customer>2</customer>
    <name>Jones</name>
    <item>1</item>
    <description>blah</description>
  </customer_item>
  <customer_item>
    <customer>2</customer>
    <name>Jones</name>
    <item>3</item>
    <description>blah3</description>
  </customer_item>
</dataset>

0 个答案:

没有答案