为XMLDocument构建DataTable和绑定到Gridview的优化方法

时间:2013-02-06 15:16:29

标签: xml azure xml-parsing linq-to-entities linq-to-xml

我尝试过很少的方法从结果xml中提取数据

<HostedService xmlns="http://schemas.microsoft.com/windowsazure">
  <Url>hosted-service-url</Url>
  <ServiceName>hosted-service-name</ServiceName>
  <HostedServiceProperties>
    <Description>description</Description>
    <Location>location</Location>
    <AffinityGroup>affinity-group</AffinityGroup>
    <Label>base-64-encoded-name-of-the-service</Label>
  </HostedServiceProperties>
  <Deployments>
    <Deployment>
      <Name>deployment-name</Name>
      <DeploymentSlot>deployment-slot</DeploymentSlot>
      <PrivateID>deployment-id</PrivateID>
      <Status>deployment-status</Status>
      <Label>base64-encoded-deployment-label</Label>
      <Url>deployment-url</Url>
      <Configuration>base-64-encoded-configuration-file</Configuration>
      <RoleInstanceList>
        <RoleInstance>
          <RoleName>role-name</RoleName>
          <InstanceName>role-instance-name</InstanceName>
          <InstanceStatus>instance-status</InstanceStatus>
        </RoleInstance>
      </RoleInstanceList>
      <UpgradeDomainCount>upgrade-domain-count</UpgradeDomainCount>
      <RoleList>
        <Role>
          <RoleName>role-name</RoleName>
          <OsVersion>operating-system-version</OsVersion>
        </Role>
      </RoleList>
      <SdkVersion>sdk-version-used-to-create-package</SdkVersion>
      <InputEndpointList>
         <InputEndpoint>
            <RoleName>role-name</RoleName>
            <Vip>virtual-ip-address</Vip>
            <Port>port-number</Port>
         </InputEndpoint>
         …
      </InputEndpointList>
      <Locked>deployment-write-allowed-status</Locked>
      <RollbackAllowed>rollback-operation-allowed</RollbackAllowed>
    </Deployment>
  </Deployments>
</HostedService>

我需要拥有上述RoleName的几个字段的数据,InstanceName Url InstanceStatus InstanceSize CreatedTime LastModifiedTime DeploymentStatus Environment IpAddress SdkVersion

以下是我做的示例方式

var result = XmlDoc.GetElementsByTagName("RoleInstance");

foreach (XmlNode node in result)
                {
                    DataRow dr = hostedserviceproperties.NewRow();



                    dr["RoleName"] = node.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "RoleName").Any() ?
                        node.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "RoleName").First().InnerText : string.Empty;
                    dr["InstanceName"] = node.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "InstanceName").Any() ?
                        node.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "InstanceName").First().InnerText : string.Empty;
                    dr["Url"] = node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Url").Any() ?
                        node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Url").First().InnerText : string.Empty;
                    dr["InstanceStatus"] = node.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "InstanceStatus").Any() ?
                        node.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "InstanceStatus").First().InnerText : string.Empty;
                    dr["InstanceSize"] = node.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "InstanceSize").Any() ?
                        node.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "InstanceSize").First().InnerText : string.Empty;
                    dr["CreatedTime"] = node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "CreatedTime").Any() ?
                        node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "CreatedTime").First().InnerText : string.Empty;
                    dr["LastModifiedTime"] = node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "LastModifiedTime").Any() ?
                        node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "LastModifiedTime").First().InnerText : string.Empty;
                    dr["DeploymentStatus"] = node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Status").Any() ?
                        node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Status").First().InnerText : string.Empty;
                    dr["Environment"] = node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "DeploymentSlot").Any() ?
                        node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "DeploymentSlot").First().InnerText : string.Empty;
                    dr["IpAddress"] = node.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "IpAddress").Any() ?
                        node.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "IpAddress").First().InnerText : string.Empty;
                    dr["SdkVersion"] = node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "SdkVersion").Any() ?
                        node.ParentNode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "SdkVersion").First().InnerText : string.Empty;
                    hostedserviceproperties.Rows.Add(dr);
                }   

在此之后,我将它作为datasource(是数据表)

提供给gridview

但我觉得这很费时间。任何更好的方法都可以更快地检索数据。

提前致谢。

0 个答案:

没有答案