1

結果の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);
                }   

この後、データソースとしてgridviewに与えました(データテーブルです)

しかし、時間がかかると感じます。データを取得するより高速な方法をもたらすより良い方法。

前もって感謝します。

4

0 に答える 0