English 中文(简体)
WCF: 如何界定数据冲突类别,以便作出极为简单的反应? A. 需要使根本内容的正本化
原标题:WCF: How to define a DataContract class for an extreamly simple response? Need to deserialize Text of the root element

I would like to call Sql Azure s REST API to create a SQL Azure server. The method is documented here: http://msdn.microsoft.com/en-us/library/gg715274.aspx I ran into problem. The response from this method is very simple: <?xml version="1.0" encoding="utf-8"?><ServerName xmlns="http://schemas.microsoft.com/sqlazure/2010/12/">zpc0fbxur0</ServerName>

How do I define a DataContract class for this response? If response were something like this: <?xml version="1.0" encoding="utf-8"?> <ServerName xmlns="http://schemas.microsoft.com/sqlazure/2010/12/"><Name>zpc0fbxur0</Name></ServerName> the following class would work:

......

    [DataContract(Namespace=SqlAzureConstants.ManagementNS, Name="ServerName")]
    public class ServerName : IExtensibleDataObject
    {
        [DataMember()]
        public string Name
        {
            get;
            set;
        }

        public ExtensionDataObject ExtensionData
        {
            get;
            set;
        }
    }

......

但是,我需要具体指出,财产应当按根本要素的案文绘制。 任何关于如何这样做的想法?

最佳回答

<代码>DataContractSerializer 既然由违约造成的,那么XML不能脱轨,但如果你使用一个包含<条码>的构造/代码>和<条码>的构造,可以照此办理。

另一种选择是使用<代码>XmlSerializer,你可以直接使用。

下面的法典显示了两种选择,也显示了使用XmlSerializer型号的网络方法。

public class StackOverflow_6399085
{
    [XmlRoot(ElementName = "ServerName", Namespace = "http://schemas.microsoft.com/sqlazure/2010/12/")]
    public class ServerName
    {
        [XmlText]
        public string Name { get; set; }

        public override string ToString()
        {
            return string.Format("ServerName[Name={0}]", this.Name);
        }
    }

    const string XML = "<?xml version="1.0" encoding="utf-8"?><ServerName xmlns="http://schemas.microsoft.com/sqlazure/2010/12/">zpc0fbxur0</ServerName>";

    static void RunWithXmlSerializer()
    {
        XmlSerializer xs = new XmlSerializer(typeof(ServerName));
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
        ServerName obj = (ServerName)xs.Deserialize(ms);
        Console.WriteLine("Using XML serializer: {0}", obj);
    }

    static void RunWithDataContractSerializer()
    {
        DataContractSerializer dcs = new DataContractSerializer(typeof(string), "ServerName", "http://schemas.microsoft.com/sqlazure/2010/12/");
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
        string name = (string)dcs.ReadObject(ms);
        Console.WriteLine("Using DataContractSerializer (different name): {0}", name);
    }

    [ServiceContract(Namespace = "http://schemas.microsoft.com/sqlazure/2010/12/")]
    public class MockSqlAzureRestService
    {
        [WebGet]
        public Stream GetServerName()
        {
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            return ms;
        }
    }

    [ServiceContract(Namespace = "http://schemas.microsoft.com/sqlazure/2010/12/")]
    public interface IServerNameClient
    {
        [WebGet(BodyStyle = WebMessageBodyStyle.Bare)]
        [XmlSerializerFormat]
        ServerName GetServerName();
    }

    static void RunWithWCFRestClient()
    {
        // Setting up the mock service
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(MockSqlAzureRestService), new Uri(baseAddress));
        host.Open();

        WebChannelFactory<IServerNameClient> factory = new WebChannelFactory<IServerNameClient>(new Uri(baseAddress));
        IServerNameClient proxy = factory.CreateChannel();
        var name = proxy.GetServerName();
        Console.WriteLine("Using WCF REST client: {0}", name);
    }

    public static void Test()
    {
        RunWithXmlSerializer();
        RunWithDataContractSerializer();
        RunWithWCFRestClient();
    }
}
问题回答

暂无回答




相关问题
Choosing the right subclass to instantiate programmatically

Ok, the context is some serialization / deserialization code that will parse a byte stream into an object representation that s easier to work with (and vice-versa). Here s a simplified example ...

WCF Problem Sending Object To Client

Background Converting from using .Net Remoting to WCF. Most of the methods on the WCF server are working fine, but ran into one that isn t working today. This is the service contract: [...

WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

deserialize the XML Document --- Need Help

I am using the below code snippet now to deserialize the XML document ... [WebMethod] public XmlDocument OrderDocument(XmlDocument xmlDoc) { XmlSerializer serializer = new XmlSerializer(typeof(...

How do I serialize a child class?

how do I include the serialized data from a child class where both impliment iserializeable? Class A Implements ISerializable dim _B as new B Class B Implements ISerializable ...

WCF: Serialize complex objects with read-only members

Looking for some guidance on a WCF service I’m prototyping. I have a WCF service hosted in IIS that will pass data to my clients. I have a separate shared assembly that contains all my business ...

Long/multiple SQL queries vs Serialization

I m trying to improve the performance of my web app where a page does a long query to pull data from different tables on a database. It pulls invoice data with multiple item lines, invoice status, and ...

热门标签