Skip to main content
 首页 » 编程设计

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

2024年02月23日18mate10pro

我已经尝试了多种尝试从 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> 

我要找回 托管服务网址, 托管服务名称, 描述, 地点, 亲和组和 标签

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

编辑:

感谢 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; 
}