日期:2014-05-18  浏览次数:20776 次

C# 解析XML
大家好,请教大家一个问题,有XML文件数据,现要对其解析,并将解析结果绑定在datagridview控件里面。这里面有个需求,请大家帮我分析下。
HTML code

<?xml version="1.0" encoding="utf-16"?>
<items>
  <totalResults>404</totalResults>
  <Rows>
    <orderCode>SC12031900028</orderCode>
    <orderCode_item>
      <Item>
        <treasuryCoding>5</treasuryCoding>
        <orderCode>SC12031900028</orderCode>
        <consignmentBrand>999999</consignmentBrand>
        <productId>LE-42S35FW</productId>
        <productName>Pangoo42寸LED 3D电视LE-42S35FW-含挂架</productName>
        <specification>42寸 3D</specification>
        <orderQuantity>1</orderQuantity>
        <shipmentsIn>1</shipmentsIn>
        <isCancel />
        <isScheduled />
        <salesPrice>0.0000</salesPrice>
        <itemStats>10</itemStats>
        <date>2012/3/19 0:00:00</date>
        <returnNum />
        <purchasePrice>3117.0000</purchasePrice>
        <returnsNum>0</returnsNum>
        <returnsNum_Arr>0</returnsNum_Arr>
        <weight>19.50</weight>
        <timeInventory>0</timeInventory>
        <inspection_Num />
        <barcode>06949791500716</barcode>
        <barcode1 />
        <refundRecord />
        <unitPrice>0.0000</unitPrice>
        <totalPrice>0.0000</totalPrice>
        <gifts_Num />
        <totalFifoCost />
        <digitalAndLibrary />
      </Item>
      <Item>
        <treasuryCoding>5</treasuryCoding>
        <orderCode>SC12031900028</orderCode>
        <consignmentBrand>999999</consignmentBrand>
        <productId>SU12030900019</productId>
        <productName>42S35一年延保</productName>
        <specification>42S35一年延保</specification>
        <orderQuantity>1</orderQuantity>
        <shipmentsIn>1</shipmentsIn>
        <isCancel />
        <isScheduled />
        <salesPrice>0.0000</salesPrice>
        <itemStats>10</itemStats>
        <date>2012/3/19 0:00:00</date>
        <returnNum />
        <purchasePrice>0.0000</purchasePrice>
        <returnsNum>0</returnsNum>
        <returnsNum_Arr>0</returnsNum_Arr>
        <weight>0.00</weight>
        <timeInventory>0</timeInventory>
        <inspection_Num />
        <barcode>TM12030900019</barcode>
        <barcode1 />
        <refundRecord />
        <unitPrice>0.0000</unitPrice>
        <totalPrice>0.0000</totalPrice>
        <gifts_Num />
        <totalFifoCost />
        <digitalAndLibrary />
      </Item>
    </orderCode_item>
  </Rows>
</items>



现在我要取xml里面的<Item></Item>里面的字段‘orderCode’‘productId’‘productName’‘orderQuantity’‘totalPrice’在datagridview显示。一个<Item></Item>在datagridview里显示一行数据,上述xml有两个<Item></Item>,请教如何将其显示两行数据?

下面是我解析的部分逻辑代码,请高手指正!

C# code

foreach (XmlNode node in xml.ChildNodes)
            {
                if (node.Name == "items")
                {
                    foreach (XmlNode node1 in node.ChildNodes)
                    {
                        if (node1.Name == "Rows")
                        {
                            foreach (XmlNode node2 in node1.ChildNodes)
                            {