簡體   English   中英

從Azure服務管理API返回的XML中提取值

[英]Extracting values from XML returned by azure service management API

我嘗試了幾種嘗試從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>

我想檢索托管服務網址,托管服務名稱,描述,位置,相似性組和標簽

檢索這些值的最佳方法是什么?

編輯:

感謝LB,該方法非常有效。 但是,我剛剛被告知,我將不得不使用下面更大的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對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/zh-cn/library /bb669152.aspx ),然后將其應用於您的案例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM