c# - 从 Azure 服务管理 API 返回的 XML 中提取值

标签 c# xml azure

我尝试了多种方法来尝试从 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>

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

如何区分它们?

最佳答案

您可以使用 Xml to 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;
}

关于c# - 从 Azure 服务管理 API 返回的 XML 中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9083888/

相关文章:

c# - 如何在不丢失 WPF 中的绑定(bind)的情况下更改 TextBox.Text?

php - Slim Framework -> 创建 XML 输出

c# - LinqToSql 中的 Xml 查询

c# - 找不到类型或 namespace MongoServer

azure - HDInsight 订阅不起作用 - Azure 数据工厂

c# - 如何在 MVC 3 中使用 LabelFor

C# - 同时声明多个数组

c# - 将 pair<int,float> 的二维数组从 c# 传递到 c dll

xml - VTD-XML 性能调优

azure - 在 Azure Web 角色中运行许多项目