我尝试了几种方法从生成的 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
但是我觉得很费时间。任何更好的方法可以更快地检索数据。
提前致谢。