小弟我的问题是将一个XML文件读取到一个GRIDVIEW中,一列为Element Name,另一列为Element Value
我想做成这样的显示格式:
Element Name      Element Value
  returnXml
  otmId           F9607DE7-0650-445E-AE00-2E42C58A0649
  methodStatus    
  statusType      success
........
请高手赐教。(不要用linq,因为小弟的这个项目是基于framework2.0的)
*******************************************************
XML 文件如下:
<?xml version="1.0" encoding="utf-8"?>
<returnXml>
  <otmId>F9607DE7-0650-445E-AE00-2E42C58A0649</otmId>
  <methodStatus>
    <statusType>success</statusType>
  </methodStatus>
  <parametersReturn>
    <company objectType="company" content="all">
      <primaryId>123</primaryId>
      <secondaryId>123</secondaryId>
      <locked>0</locked>
      <assignedId></assignedId>
      <emailAddress>[email protected]</emailAddress>
      <url></url>
      <companyName>TestCompany123</companyName>
      <sourceId null="1"></sourceId>
      <statusId null="1"></statusId>
      <companyTypeCode null="1"></companyTypeCode>
      <companySubTypeCode null="1"></companySubTypeCode>
      <familyId null="1"></familyId>
      <parentId null="1"></parentId>
      <parentName null="1"></parentName>
      <primaryContactId>0</primaryContactId>
      <contactFirstName></contactFirstName>
      <contactLastName></contactLastName>
      <divisionCode null="1"></divisionCode>
      <sicCode null="1"></sicCode>
      <etSector null="1"></etSector>
      <taxId null="1"></taxId>
      <dunnsNumber null="1"></dunnsNumber>
      <privateAccess>0</privateAccess>
      <readOnlyAccess>0</readOnlyAccess>
      <insertBy>wstest1</insertBy>
      <insertDate>2008-05-22 12:33:44</insertDate>
      <updateBy>wstest1</updateBy>
      <updateDate>2008-07-04 17:30:53</updateDate>
      <lockedBy null="1"></lockedBy>
      <alertId>0</alertId>
      <user1 null="1"></user1>
      <user2 null="1"></user2>
      <user3 null="1"></user3>
      <user4 null="1"></user4>
      <user5 null="1"></user5>
      <user6 null="1"></user6>
      <user7 null="1"></user7>
      <user8 null="1"></user8>
      <user9 null="1"></user9>
      <user10 null="1"></user10>
      <onyxTimestamp>2008-07-04 17:30:53</onyxTimestamp>
      <phones objectType="phone" collection="1">
        <phone objectType="phone" content="all">
          <primaryId></primaryId>
          <primary>1</primary>
          <ownerId>123</ownerId>
          <ownerType>2</ownerType>
          <phoneTypeId null="1"></phoneTypeId>
          <phoneNumber></phoneNumber>
          <readOnlyAccess></readOnlyAccess>
          <privateAccess></privateAccess>
          <insertBy></insertBy>
          <insertDate></insertDate>
          <updateBy></updateBy>
          <updateDate></updateDate>
          <locked></locked>
          <onyxTimestamp></onyxTimestamp>
        </phone>
      </phones>
      <addresses objectType="address" collection="1">
        <address objectType="address" content="all">
          <primaryId></primaryId>
          <primary>1</primary>
          <ownerId>123</ownerId>
          <ownerType>2</ownerType>
          <addressTypeId null="1"></addressTypeId>
          <salutation></salutation>
          <firstName></firstName>
          <middleName></middleName>
          <lastName></lastName>
          <suffix></suffix>
          <companyName></companyName>
          <address1 null="1"></address1>
          <address2 null="1"></address2>
          <address3 null="1"></address3>
          <address4></address4>
          <address5></address5>
          <city></city>
          <regionCode></regionCode>
          <countryCode></countryCode>
          <postCode></postCode>
          <validAddress>0</validAddress>
          <readOnlyAccess></readOnlyAccess>
          <privateAccess></privateAccess>
          <private>0</private>
          <insertBy></insertBy>
          <insertDate></insertDate>
          <updateBy></updateBy>
          <updateDate></updateDate>
          <locked></locked>
          <onyxTimestamp></onyxTimestamp>
        </address>
      </addresses>
    </company>
  </parametersReturn>
  <customData></customData>
</returnXml>

解决方案 »

  1.   

    读出所有节点,用递归把每个节点名称和值添加到一个datatable 中就OK
      

  2.   

    http://hi.baidu.com/beebon/blog/item/21b659d9f49d65eb38012f6e.html
      

  3.   

        protected void Page_Load(object sender, EventArgs e)
            {            XmlDocument dom = new XmlDocument();
                dom.Load(Server.MapPath("a.xml"));            DataTable dt = new DataTable();
                dt.Columns.Add("ElementName");
                dt.Columns.Add("ElementValue");            XmlNode node = dom.DocumentElement.SelectSingleNode("//returnXml");            ReadValue(node, ref dt);
                GridView1.DataSource = dt.DefaultView;
                GridView1.DataBind();
                
        
        }
            void ReadValue(XmlNode node, ref DataTable dt)
            {
                 
                dt.Rows.Add(node.Name, node.InnerText);
               
                foreach (XmlNode cNode in node.ChildNodes)
                {
                    if (cNode.Name != "#text")
                    {
                        ReadValue(cNode, ref dt);
                    }
                }
            }