从azure服务管理API返回的XML中提取值

本文关键字:XML 提取 返回 azure 服务 管理 API | 更新日期: 2023-09-27 18:21:57

我尝试过几种从XML文件中提取值的方法,但似乎都不起作用。我正在使用C#。XML如下

<?xml version="1.0" encoding="utf-8"?>
<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>label</Label>
  </HostedServiceProperties>
</HostedService>

我想取回托管服务url,托管服务名称,描述地方亲和群和标签

检索这些值的最佳方式是什么?

编辑:

谢谢L.B,这种方法非常有效。然而,我刚刚被告知,我将不得不使用下面更大的XML。

<?xml version="1.0" encoding="utf-8"?>
<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>

我的最后一个问题是,有几个重复的标签,比如,

我如何区分它们?

从azure服务管理API返回的XML中提取值

您可以使用Xml到Linq来解析您的Xml字符串。例如

var xElem = XElement.Load(new StringReader(xml));
var ns = XNamespace.Get("http://schemas.microsoft.com/windowsazure");
var obj = new
    {
        ServiceName = xElem.Descendants(ns + "ServiceName").First().Value,
        Description = xElem.Descendants(ns + "Description").First().Value,
    };

或者可以使用XmlSerializer

XmlSerializer xs = new XmlSerializer(typeof(HostedService), "http://schemas.microsoft.com/windowsazure");
var obj2 = (HostedService)xs.Deserialize(new StringReader(xml));

public class HostedService
{
    public string Url;
    public string ServiceName;
    public HostedServiceProperties HostedServiceProperties;
}
public class HostedServiceProperties
{
    public string Description;
    public string Location;
    public string AffinityGroup;
    public string Label;
}

也许您可以尝试XmlDocument(http://msdn.microsoft.com/en-us/library/d271ytdx.aspx)和LINQ to XML-(http://msdn.microsoft.com/en-us/library/bb669152.aspx)首先,然后将其应用于您的案例。