从 azure 响应中反序列化 xml

deserialize xml from azure response

我正在寻找反序列化数据并将其从 response from Azure.

放入通用 class
<ServiceResources xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/windowsazure">
<ServiceResource>
    <Name>Airport1</Name>
    <Type>Microsoft.SqlAzure.FirewallRule</Type>
    <State>Normal</State>
    <SelfLink>https://management.core.windows.net:xxx/xxx/services/sqlservers/servers/xxx/firewallrules/Airport1</SelfLink>
    <ParentLink>https://management.core.windows.net:xxxx/services/sqlservers/servers/xxx</ParentLink>
    <StartIPAddress>000.000.000.000</StartIPAddress>
    <EndIPAddress>2000.000.000.000</EndIPAddress>
  </ServiceResource>

有几个对象需要反序列化到我的class。

[Serializable, XmlRoot(ElementName = "ServiceResource", Namespace = "http://schemas.microsoft.com/windowsazure/")]
public class ServiceResource
{
    [XmlElement("Name")]
    public string Name { get; set; }
    [XmlElement("Type")]
    public string Type { get; set; }
    [XmlElement("State")]
    public string State { get; set; }
    [XmlElement("SelfLink")]
    public string SelfLink { get; set; }
    [XmlElement("ParentLink")]
    public string ParentLink { get; set; }
    [XmlElement("StartIPAddress")]
    public string StartIPAddress { get; set; }
    [XmlElement("EndIPAddress")]
    public string EndIPAddress { get; set; }
} 

我已经尝试了几种不同的尝试,但都无法成功。我已经使用了 xmlSerializer 但遇到了阻碍。

using (var responseStreamReader = new StreamReader(webResponse.GetResponseStream()))
{
    XmlSerializer serializer = new XmlSerializer(typeof(ServiceResource));
    ServiceResource deserialized = (ServiceResource)serializer.Deserialize(responseStreamReader);

}

如有任何帮助,我们将不胜感激。

回答

Azure REST Api 正在返回 XML 中的 ServiceResource 列表。所以你需要把它封装成一个class。这是一个例子。

[XmlRoot(
    ElementName = "ServiceResources",
    Namespace = "http://schemas.microsoft.com/windowsazure")]
public class ServiceResources
{
    public ServiceResources()
    {
        Items = new List<ServiceResource>();
    }

    [XmlElement("ServiceResource")]
    public List<ServiceResource> Items { get; set; }
}

public class ServiceResource
{
    [XmlElement("Name")]
    public string Name { get; set; }
    [XmlElement("Type")]
    public string Type { get; set; }
    [XmlElement("State")]
    public string State { get; set; }
    [XmlElement("SelfLink")]
    public string SelfLink { get; set; }
    [XmlElement("ParentLink")]
    public string ParentLink { get; set; }
    [XmlElement("StartIPAddress")]
    public string StartIPAddress { get; set; }
    [XmlElement("EndIPAddress")]
    public string EndIPAddress { get; set; }
}

有了这两个 classes,您现在可以执行以下操作。

var response = request.GetResponse();
var message = string.Empty;
using (var responseStreamReader = new StreamReader(response.GetResponseStream()))
{
    message = responseStreamReader.ReadToEnd();
}

var textReader = new StringReader(message);
var serializer = new XmlSerializer(typeof(ServiceResources));
var serviceResources = 
    serializer.Deserialize(textReader) as ServiceResources;

演示控制台应用程序

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace DeserializeAzureXmlResponse
{
    class Program
    {
        private static string certificateThumbprint = "19DAED4D4ABBE0D400DC33A6D99D00D7BBB24472";
        private static string subscriptionId = "14929cfc-3501-48cf-a5c9-b24a7daaf694";
        static string sqlServerName = "mvp2015";

        static string managementUri = "https://management.core.windows.net";
        static string sqlServerApi = "services/sqlservers/servers";
        static string firewallRules = "firewallrules";

        static void Main(string[] args)
        {
            var restUri = CreateRestUri();
            var clientCert = GetX509FromPersonalStore();

            var request = (HttpWebRequest)HttpWebRequest.Create(restUri);
            request.Headers.Add("x-ms-version", "2012-03-01");
            request.ClientCertificates.Add(clientCert);

            var response = request.GetResponse();
            var message = string.Empty;
            using (var responseStreamReader = new StreamReader(response.GetResponseStream()))
            {
                message = responseStreamReader.ReadToEnd();
            }

            var textReader = new StringReader(message);
            var serializer = new XmlSerializer(typeof(ServiceResources));
            var serviceResources = serializer.Deserialize(textReader) as ServiceResources;
            foreach (var sr in serviceResources.Items)
            {
                Console.WriteLine("Name".PadRight(20) + sr.Name);
                Console.WriteLine("Type".PadRight(20) + sr.Type);
                Console.WriteLine("State".PadRight(20) + sr.State);
                Console.WriteLine("SelfLink".PadRight(20) + sr.SelfLink);
                Console.WriteLine("ParentLink".PadRight(20) + sr.ParentLink);
                Console.WriteLine("StartIP".PadRight(20) + sr.StartIPAddress);
                Console.WriteLine("EndIP".PadRight(20) + sr.EndIPAddress);
                Console.WriteLine("+++++++++++");
            }
            Console.ReadLine();
        }

        static Uri CreateRestUri()
        {
            // https://management.core.windows.net/{subscriptionID}/services/sqlservers/servers/{server}/firewallrules/
            var builder = new StringBuilder();
            builder.Append(managementUri + "/");
            builder.Append(subscriptionId + "/");
            builder.Append(sqlServerApi + "/");
            builder.Append(sqlServerName + "/");
            builder.Append(firewallRules + "/");
            var uri = new Uri(builder.ToString());
            return uri;
        }

        static X509Certificate GetX509FromPersonalStore()
        {
            // To view the personal store, press `Win + R` and then type `certmgr.msc`
            var store = new X509Store(StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, certificateThumbprint, true);
            var certificate = certificates[0];
            store.Close();
            return certificate;
        }
    }

    [XmlRoot(
        ElementName = "ServiceResources",
        Namespace = "http://schemas.microsoft.com/windowsazure")]
    public class ServiceResources
    {
        public ServiceResources()
        {
            Items = new List<ServiceResource>();
        }

        [XmlElement("ServiceResource")]
        public List<ServiceResource> Items { get; set; }
    }

    public class ServiceResource
    {
        [XmlElement("Name")]
        public string Name { get; set; }
        [XmlElement("Type")]
        public string Type { get; set; }
        [XmlElement("State")]
        public string State { get; set; }
        [XmlElement("SelfLink")]
        public string SelfLink { get; set; }
        [XmlElement("ParentLink")]
        public string ParentLink { get; set; }
        [XmlElement("StartIPAddress")]
        public string StartIPAddress { get; set; }
        [XmlElement("EndIPAddress")]
        public string EndIPAddress { get; set; }
    }
}

输出

Name                My-House
Type                Microsoft.SqlAzure.FirewallRule
State               Normal
SelfLink            https://management.core.windows.net/14929cfc-35
ParentLink          https://management.core.windows.net/14929cfc-35
StartIP             123.435.234.643
EndIP               123.435.234.643
+++++++++++
Name                AllowAllWindowsAzureIps
Type                Microsoft.SqlAzure.FirewallRule
State               Normal
SelfLink            https://management.core.windows.net/14929cfc-35
ParentLink          https://management.core.windows.net/14929cfc-35
StartIP             0.0.0.0
EndIP               0.0.0.0
+++++++++++

另请参阅

Is it possible to deserialize XML into List<T>?

List Firewall Rules

我假设您正在尝试反序列化整个对象图。给定 xml 的根节点 ServiceResources 包含 ServiceResource。你有两个选择,你可以将整个 xml 模拟成 classes 并反序列化;或者只是获取 ServiceResource 的内部节点并反序列化该部分。

如果您使用第一个选项,那么您需要将 ServiceResource 存储在另一个 class 中,该 class 映射集合 属性,XmlElement 名称设置为 "ServiceResource",例如:

[XmlType(Namespace="http://schemas.microsoft.com/windowsazure")]
[XmlRoot(Namespace="http://schemas.microsoft.com/windowsazure")]        
public class ServiceResource
{
    public string Name { get; set; }

    public string Type { get; set; }

    public string State { get; set; }

    public string SelfLink { get; set; }

    public string ParentLink { get; set; }

    public string StartIPAddress { get; set; }

    public string EndIPAddress { get; set; }

}

[XmlType(Namespace="http://schemas.microsoft.com/windowsazure")]
[XmlRoot(Namespace="http://schemas.microsoft.com/windowsazure")]
public class ServiceResources
{
    [XmlElement("ServiceResource")]
    public List<ServiceResource> ServiceResource { get; set; }
}

这样你就可以直接反序列化了。 Container class 将集合映射到 ServiceResource 元素,这将加载服务资源的所有节点。注意:反序列化目标类型现在是 "ServiceResources" 而不是内部类型 "ServiceResource"

using (var responseStreamReader = new StreamReader(webResponse.GetResponseStream()))
{
    XmlSerializer serializer = new XmlSerializer(typeof(ServiceResources));
    ServiceResources deserialized = (ServiceResources)serializer.Deserialize(responseStreamReader);

     //you can access each item in loop
    foreach(var res in deserialized.ServiceResource)
    {
       //access items e.g.
       var name = res.Name;
    }
}