嵌套的XmlDocument / XML到DataTable或C#中的DataSet转换

时间:2014-01-17 12:45:34

标签: c# asp.net xml linq

我有一个XML文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<root type="object">
<product_uuid type="string">fd2ae1d7-9f94-49a0-90a7-00eddfc9beec</product_uuid>
<product_code type="string">BSLR-ROLB-1.5X1.5</product_code>
<product_description type="string">1.5" x 1.5" Bright Silver Metallic Roll Label</product_description>
<categories type="array">
  <item type="object">
     <category_uuid type="string">a2b13bce-0643-41ce-9a03-e21f9a92d7d4</category_uuid>
     <category_name type="string">Roll Labels</category_name>
  </item>
</categories>
<product_option_groups type="array">
  <item type="object">
     <product_option_group_uuid type="string">34f407f8-0b50-4227-9378-10fddefbe596</product_option_group_uuid>
     <product_option_group_name type="string">Size</product_option_group_name>
     <minoccurs type="string">1</minoccurs>
     <maxoccurs type="string">1</maxoccurs>
     <options type="array">
        <item type="object">
           <option_uuid type="string">213c078a-49d0-40e4-8667-bacbcd12672b</option_uuid>
           <option_name type="string">1.5" x 1.5"</option_name>
           <option_description type="string" />
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/34f407f8-0b50-4227-9378-10fddefbe596/options/213c078a-49d0-40e4-8667-bacbcd12672b/prices</option_prices>
        </item>
     </options>
  </item>
  <item type="object">
     <product_option_group_uuid type="string">24865ffa-793d-43ea-b3b1-d1b5cf22268d</product_option_group_uuid>
     <product_option_group_name type="string">Stock</product_option_group_name>
     <minoccurs type="string">1</minoccurs>
     <maxoccurs type="string">1</maxoccurs>
     <options type="array">
        <item type="object">
           <option_uuid type="string">89e4c924-3fc4-44c5-8254-f630aaa15fc5</option_uuid>
           <option_name type="string">Bright Silver Metallic</option_name>
           <option_description type="string">Bright Silver</option_description>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/24865ffa-793d-43ea-b3b1-d1b5cf22268d/options/89e4c924-3fc4-44c5-8254-f630aaa15fc5/prices</option_prices>
        </item>
     </options>
  </item>
  <item type="object">
     <product_option_group_uuid type="string">26ca0df3-0682-4f37-8979-409868e2df2d</product_option_group_uuid>
     <product_option_group_name type="string">Coating</product_option_group_name>
     <minoccurs type="string">1</minoccurs>
     <maxoccurs type="string">1</maxoccurs>
     <options type="array">
        <item type="object">
           <option_uuid type="string">5a5cf5bf-2e02-4675-9391-f6a8370ad0e8</option_uuid>
           <option_name type="string">N/A</option_name>
           <option_description type="string" />
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/26ca0df3-0682-4f37-8979-409868e2df2d/options/5a5cf5bf-2e02-4675-9391-f6a8370ad0e8/prices</option_prices>
        </item>
     </options>
  </item>
  <item type="object">
     <product_option_group_uuid type="string">38d33954-5a42-4112-a905-215eb827e62c</product_option_group_uuid>
     <product_option_group_name type="string">Colorspec</product_option_group_name>
     <minoccurs type="string">1</minoccurs>
     <maxoccurs type="string">1</maxoccurs>
     <options type="array">
        <item type="object">
           <option_uuid type="string">32d3c223-f82c-492b-b915-ba065a00862f</option_uuid>
           <option_name type="string">4/0</option_name>
           <option_description type="string" />
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/38d33954-5a42-4112-a905-215eb827e62c/options/32d3c223-f82c-492b-b915-ba065a00862f/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">a75ac2f1-9911-48d6-841d-245b5ac08f27</option_uuid>
           <option_name type="string">5/0</option_name>
           <option_description type="string" />
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/38d33954-5a42-4112-a905-215eb827e62c/options/a75ac2f1-9911-48d6-841d-245b5ac08f27/prices</option_prices>
        </item>
     </options>
  </item>      
  <item type="object">
     <product_option_group_uuid type="string">5fffd29b-af78-41af-a216-7cdaf8d55177</product_option_group_uuid>
     <product_option_group_name type="string">Shape Options</product_option_group_name>
     <minoccurs type="string">1</minoccurs>
     <maxoccurs type="string">1</maxoccurs>
     <options type="array">
        <item type="object">
           <option_uuid type="string">d38a0067-50a0-4d95-bdb9-8a5a57424906</option_uuid>
           <option_name type="string">Circle Roll Label</option_name>
           <option_description type="string">Shape Options</option_description>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/5fffd29b-af78-41af-a216-7cdaf8d55177/options/d38a0067-50a0-4d95-bdb9-8a5a57424906/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">b9ca1532-e9f1-4a16-930d-0b4a0693c1e9</option_uuid>
           <option_name type="string">Square Roll Label</option_name>
           <option_description type="string">Shape Options</option_description>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/5fffd29b-af78-41af-a216-7cdaf8d55177/options/b9ca1532-e9f1-4a16-930d-0b4a0693c1e9/prices</option_prices>
        </item>
     </options>
  </item>
  <item type="object">
     <product_option_group_uuid type="string">5d3e6a6f-403b-4c57-9af1-499d38873b75</product_option_group_uuid>
     <product_option_group_name type="string">Unwind Options</product_option_group_name>
     <minoccurs type="string">1</minoccurs>
     <maxoccurs type="string">1</maxoccurs>
     <options type="array">
        <item type="object">
           <option_uuid type="string">d16c2772-aac7-41d6-a124-047cd7375882</option_uuid>
           <option_name type="string">Not Important</option_name>
           <option_description type="string">Unwind Options</option_description>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/5d3e6a6f-403b-4c57-9af1-499d38873b75/options/d16c2772-aac7-41d6-a124-047cd7375882/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">9ec9c574-00b8-4b05-9945-b46087253bbc</option_uuid>
           <option_name type="string">Off Bottom (Direction #2)</option_name>
           <option_description type="string">Unwind Options</option_description>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/5d3e6a6f-403b-4c57-9af1-499d38873b75/options/9ec9c574-00b8-4b05-9945-b46087253bbc/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">d1d7deb3-994a-49b6-9c93-3c98d66faa44</option_uuid>
           <option_name type="string">Off Left (Direction #4)</option_name>
           <option_description type="string">Unwind Options</option_description>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/5d3e6a6f-403b-4c57-9af1-499d38873b75/options/d1d7deb3-994a-49b6-9c93-3c98d66faa44/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">a1149bbd-f1ff-4ee8-b7b7-c656c29710cd</option_uuid>
           <option_name type="string">Off Right (Direction #3)</option_name>
           <option_description type="string">Unwind Options</option_description>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/5d3e6a6f-403b-4c57-9af1-499d38873b75/options/a1149bbd-f1ff-4ee8-b7b7-c656c29710cd/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">8b1a0725-314d-43f7-9ebe-309d4b43d313</option_uuid>
           <option_name type="string">Off Top (Direction #1)</option_name>
           <option_description type="string">Unwind Options</option_description>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/5d3e6a6f-403b-4c57-9af1-499d38873b75/options/8b1a0725-314d-43f7-9ebe-309d4b43d313/prices</option_prices>
        </item>
     </options>
  </item>
  <item type="object">
     <product_option_group_uuid type="string">d45cec1f-d12c-45c8-b2fa-9297efd29524</product_option_group_uuid>
     <product_option_group_name type="string">Turnaround-times</product_option_group_name>
     <minoccurs type="number">1</minoccurs>
     <maxoccurs type="number">1</maxoccurs>
     <options type="array">
        <item type="object">
           <option_uuid type="string">ad290b77-c9d5-476c-b847-9ad4d25ae14e</option_uuid>
           <option_name type="string">121</option_name>
           <option_description type="string">Turnaround-time Next Business Day</option_description>
           <runsize_uuid type="string">6237a36b-b046-4ef6-8fed-6cb9c22a5ece</runsize_uuid>
           <runsize type="string">250</runsize>
           <colorspec_uuid type="string">32d3c223-f82c-492b-b915-ba065a00862f</colorspec_uuid>
           <colorspec type="string">4/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/188bae57-e363-4592-a8b3-2871ecd50ae0/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">ad290b77-c9d5-476c-b847-9ad4d25ae14e</option_uuid>
           <option_name type="string">121</option_name>
           <option_description type="string">Turnaround-time Next Business Day</option_description>
           <runsize_uuid type="string">6237a36b-b046-4ef6-8fed-6cb9c22a5ece</runsize_uuid>
           <runsize type="string">250</runsize>
           <colorspec_uuid type="string">a75ac2f1-9911-48d6-841d-245b5ac08f27</colorspec_uuid>
           <colorspec type="string">5/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/6e55bae9-20bd-4054-bbf3-2f8d5c58d165/prices</option_prices>
        </item>            
        <item type="object">
           <option_uuid type="string">ad290b77-c9d5-476c-b847-9ad4d25ae14e</option_uuid>
           <option_name type="string">121</option_name>
           <option_description type="string">Turnaround-time Next Business Day</option_description>
           <runsize_uuid type="string">52e3d710-0e8f-4d4d-8560-7d4d8655be69</runsize_uuid>
           <runsize type="string">1000</runsize>
           <colorspec_uuid type="string">a75ac2f1-9911-48d6-841d-245b5ac08f27</colorspec_uuid>
           <colorspec type="string">5/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/58ca3026-cc8b-4c30-ac93-ef2b0f0589b5/prices</option_prices>
        </item>           
        <item type="object">
           <option_uuid type="string">ad290b77-c9d5-476c-b847-9ad4d25ae14e</option_uuid>
           <option_name type="string">121</option_name>
           <option_description type="string">Turnaround-time Next Business Day</option_description>
           <runsize_uuid type="string">4c29185a-ec98-4488-8729-c2ae0e5f5fe1</runsize_uuid>
           <runsize type="string">10000</runsize>
           <colorspec_uuid type="string">32d3c223-f82c-492b-b915-ba065a00862f</colorspec_uuid>
           <colorspec type="string">4/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/d3ff8633-af18-4ce8-aef0-f9fc370d6579/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">ad290b77-c9d5-476c-b847-9ad4d25ae14e</option_uuid>
           <option_name type="string">121</option_name>
           <option_description type="string">Turnaround-time Next Business Day</option_description>
           <runsize_uuid type="string">4c29185a-ec98-4488-8729-c2ae0e5f5fe1</runsize_uuid>
           <runsize type="string">10000</runsize>
           <colorspec_uuid type="string">a75ac2f1-9911-48d6-841d-245b5ac08f27</colorspec_uuid>
           <colorspec type="string">5/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/75002c77-0eee-42cf-9d9c-55c06ddb4bcd/prices</option_prices>
        </item>           
        <item type="object">
           <option_uuid type="string">9f85d0c4-d344-4088-a719-86f8f84d504d</option_uuid>
           <option_name type="string">224</option_name>
           <option_description type="string">Turnaround-time 2-4 Business Days</option_description>
           <runsize_uuid type="string">e824bf9f-d22d-4070-926d-42c6dd5ef515</runsize_uuid>
           <runsize type="string">5000</runsize>
           <colorspec_uuid type="string">32d3c223-f82c-492b-b915-ba065a00862f</colorspec_uuid>
           <colorspec type="string">4/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/8af47a02-9144-47da-9350-c207ef78c5d5/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">9f85d0c4-d344-4088-a719-86f8f84d504d</option_uuid>
           <option_name type="string">224</option_name>
           <option_description type="string">Turnaround-time 2-4 Business Days</option_description>
           <runsize_uuid type="string">e824bf9f-d22d-4070-926d-42c6dd5ef515</runsize_uuid>
           <runsize type="string">5000</runsize>
           <colorspec_uuid type="string">a75ac2f1-9911-48d6-841d-245b5ac08f27</colorspec_uuid>
           <colorspec type="string">5/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/fd6a052d-16b0-4b4e-afa7-c9bc759ac985/prices</option_prices>
        </item>           
        <item type="object">
           <option_uuid type="string">9f85d0c4-d344-4088-a719-86f8f84d504d</option_uuid>
           <option_name type="string">224</option_name>
           <option_description type="string">Turnaround-time 2-4 Business Days</option_description>
           <runsize_uuid type="string">8055b2b4-3fe2-4c57-beaf-a64ec10aed49</runsize_uuid>
           <runsize type="string">15000</runsize>
           <colorspec_uuid type="string">a75ac2f1-9911-48d6-841d-245b5ac08f27</colorspec_uuid>
           <colorspec type="string">5/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/fdcff6c9-fdde-4a2f-b009-a64f7111486f/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">9f85d0c4-d344-4088-a719-86f8f84d504d</option_uuid>
           <option_name type="string">224</option_name>
           <option_description type="string">Turnaround-time 2-4 Business Days</option_description>
           <runsize_uuid type="string">889aa9a1-d0cc-4fb2-869c-73e1fe60855b</runsize_uuid>
           <runsize type="string">20000</runsize>
           <colorspec_uuid type="string">32d3c223-f82c-492b-b915-ba065a00862f</colorspec_uuid>
           <colorspec type="string">4/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/fd330607-5841-4295-8c52-5b81d8a07d8c/prices</option_prices>
        </item>            
        <item type="object">
           <option_uuid type="string">50979118-b3a2-4556-9f46-d1da268f2354</option_uuid>
           <option_name type="string">222</option_name>
           <option_description type="string">Turnaround-time 2 Business Days</option_description>
           <runsize_uuid type="string">8055b2b4-3fe2-4c57-beaf-a64ec10aed49</runsize_uuid>
           <runsize type="string">15000</runsize>
           <colorspec_uuid type="string">a75ac2f1-9911-48d6-841d-245b5ac08f27</colorspec_uuid>
           <colorspec type="string">5/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/fd19c941-5610-4316-be70-a9122e6b99ed/prices</option_prices>
        </item>
        <item type="object">
           <option_uuid type="string">50979118-b3a2-4556-9f46-d1da268f2354</option_uuid>
           <option_name type="string">222</option_name>
           <option_description type="string">Turnaround-time 2 Business Days</option_description>
           <runsize_uuid type="string">889aa9a1-d0cc-4fb2-869c-73e1fe60855b</runsize_uuid>
           <runsize type="string">20000</runsize>
           <colorspec_uuid type="string">32d3c223-f82c-492b-b915-ba065a00862f</colorspec_uuid>
           <colorspec type="string">4/0</colorspec>
           <option_prices type="string">http://sandbox-api.4over.com/printproducts/products/fd2ae1d7-9f94-49a0-90a7-00eddfc9beec/optiongroups/d45cec1f-d12c-45c8-b2fa-9297efd29524/options/0f4dedc2-83cb-40b7-bf5e-2ee9da805426/prices</option_prices>
        </item>           
     </options>
  </item>
  </product_option_groups>
  </root>

我想将此文件转换为数据集或数据表,请帮助我。 目前我正在使用一些代码,但这是一个错误,即 “表(项)在嵌套关系中不能是子表。”

谢谢&amp;问候, 维杰普拉塔普

2 个答案:

答案 0 :(得分:0)

  1. 创建xslt,将您的xml修改为DataSet熟悉的xml格式

  2. 应用xslt to your xml

  3. 使用DataSet.Read方法使用结果xml

答案 1 :(得分:0)

xml不是DataSet可接受的格式。最好为它创建一个XSLT,然后尝试。