简体   繁体   English

如何在XML中对C#WCF DataContract进行序列化/反序列化

[英]How to serialize/deserialize a C# WCF DataContract to/from XML

I am developing a WCF service which will be consumed by multiple different client applications. 我正在开发WCF服务,该服务将由多个不同的客户端应用程序使用。 In order to make one functionality work, the server needs to read an XML file into a C# DataContract which is then passed on to the concerned client. 为了使一种功能起作用,服务器需要将XML文件读取到C#DataContract中,然后将其传递给有关的客户端。 As far as I understand from the MSDN website, this is possible but I couldn't find any complete examples. 据我从MSDN网站了解,这是可能的,但我找不到完整的示例。 In particular, the website talks about a 'stream' parameter which I don't quite get yet. 特别是,该网站谈论的是“流”参数,我还不太了解。

My data contract has one property field which is a list of another data contract which has multiple simple property fields. 我的数据合同具有一个属性字段,该属性字段是另一个数据合同的列表,该数据合同具有多个简单的属性字段。

eg 例如

    [DataContract]
    public class MyClass1 {
        [DataMember]
        public string name;
        [DataMember]
        public int age;
    }

    [DataContract]
    public class MyClass2 {
        [DataMember]
        public List<MyClass1> myClass1List;
    }

My classes look something like this. 我的课看起来像这样。

Here is an example 这是一个例子

MyClass1 obj = new MyClass1();
DataContractSerializer dcs = new DataContractSerializer(typeof(MyClass1));

using (Stream stream = new FileStream(@"C:\tmp\file.xml", FileMode.Create, FileAccess.Write))
{
    using (XmlDictionaryWriter writer = 
        XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8))
    {
        writer.WriteStartDocument();
        dcs.WriteObject(writer, obj);
    }
}

Books b = new Books();

DataContractSerializer dcs = new DataContractSerializer(typeof(Books));

try
{
    Stream fs = new FileStream(@"C:\Users\temelm\Desktop\XmlFile.xml", FileMode.Create, FileAccess.Write);

    XmlDictionaryWriter xdw = XmlDictionaryWriter.CreateTextWriter(fs, Encoding.UTF8);
    xdw.WriteStartDocument();
    dcs.WriteObject(xdw, b);
    xdw.Close();
    fs.Flush();
    fs.Close();
}
catch (Exception e)
{
    s += e.Message + "\n";
}

This can be useful for you. 这对您可能有用。 When you need XElement. 当您需要XElement时。 For instance when you going append node to XDocument or replece XElement of this document. 例如,当您将节点追加到XDocument或补充该文档的XElement时。

private XElement objectToXElement(SomeContractType obj)
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(SomeContractType);

            var ms = new MemoryStream();
            var xw = XmlWriter.Create(ms);
            dcs.WriteObject(xw, obj);
            xw.Flush();
            xw.Close();
            ms.Position = 0;
            XElement xe = XElement.Load(ms);

            return xe;
        }

There is the NetDataContractSerializer which solves a whole bunch of problems when using WCF. NetDataContractSerializer解决了使用WCF时的许多问题。

See here MSDN NetDataContractSerializer 在这里看到MSDN NetDataContractSerializer

It is typically used for wrapping all kinds of objects and pass it over WCF. 它通常用于包装各种对象并通过WCF传递。

You can use it for wrapping objects into a byte[] and transport it over WCF, on the serverside, you can easily Deserialize the objects and do whatever you want with them. 您可以将其用于将对象包装到byte[] ,并通过WCF进行传输,在服务器端,可以轻松地反序列化对象并对它们进行任何处理。

Here is a discussion on how to use this Serializer correctly: MSDN Social 这是有关如何正确使用此序列化程序的讨论: MSDN Social

Code snippets are provided there also! 那里也提供代码片段!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM