我的数据合同有一个属性字段,它是具有多个简单属性字段的另一个数据契约的列表.
例如
[DataContract] public class MyClass1 { [DataMember] public string name; [DataMember] public int age; } [DataContract] public class MyClass2 { [DataMember] public List<MyClass1> myClass1List; }
我的课程看起来像这样.
解决方法 这是一个例子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",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";}总结
以上是内存溢出为你收集整理的如何将C#WCF DataContract序列化/反序列化到/从XML全部内容,希望文章能够帮你解决如何将C#WCF DataContract序列化/反序列化到/从XML所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)