- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Xml 消息列表,特别是我记录到文件中的 DataContract 消息。我正试图从文件中一个一个地反序列化它们。我不想一次将整个文件读入内存,因为我预计它会非常大。
我有一个这个序列化的实现并且有效。我通过使用 FileStream 进行序列化并读取字节并使用正则表达式来确定元素的结尾来做到这一点。然后获取元素并使用 DataContractSerializer 获取实际对象。
但我被告知我应该使用更高级别的代码来完成这项任务,而且看起来这应该是可能的。我有以下代码,我认为它应该可以工作,但它没有。
FileStream readStream = File.OpenRead(filename);
DataContractSerializer ds = new DataContractSerializer(typeof(MessageType));
MessageType msg;
while ((msg = (MessageType)ds.ReadObject(readStream)) != null)
{
Console.WriteLine("Test " + msg.Property1);
}
上面的代码由一个包含以下内容的输入文件提供:
<MessageType>....</MessageType>
<MessageType>....</MessageType>
<MessageType>....</MessageType>
看来我可以正确读取和反序列化第一个元素,但之后它失败了:
System.Runtime.Serialization.SerializationException was unhandled
Message=There was an error deserializing the object of type MessageType. The data at the root level is invalid. Line 1, position 1.
Source=System.Runtime.Serialization
我在某处读到这是由于 DataContractSerializer 与填充的 '\0' 一起工作到最后的方式 - 但我无法弄清楚从流中读取时如何解决这个问题而没有弄清楚结尾MessageType 标记以其他方式。我应该使用另一个序列化类吗?还是解决这个问题的方法?
谢谢!
最佳答案
当您反序列化文件中的数据时,WCF 默认使用只能使用适当的 XML 文档的读取器。您正在阅读的文档不是 - 它包含多个根元素,因此它实际上是一个片段。您可以通过使用 ReadObject
的另一个重载来更改序列化程序正在使用的阅读器,如下例所示,到一个接受片段的(通过使用 XmlReaderSettings
对象)。或者您可以在 <MessageType>
周围使用某种包装元素元素,您会一直阅读,直到读者定位到包装器的末尾元素。
public class StackOverflow_7760551
{
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
public override string ToString()
{
return string.Format("Person[Name={0},Age={1}]", this.Name, this.Age);
}
}
public static void Test()
{
const string fileName = "test.xml";
using (FileStream fs = File.Create(fileName))
{
Person[] people = new Person[]
{
new Person { Name = "John", Age = 33 },
new Person { Name = "Jane", Age = 28 },
new Person { Name = "Jack", Age = 23 }
};
foreach (Person p in people)
{
XmlWriterSettings ws = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
OmitXmlDeclaration = true,
Encoding = new UTF8Encoding(false),
CloseOutput = false,
};
using (XmlWriter w = XmlWriter.Create(fs, ws))
{
DataContractSerializer dcs = new DataContractSerializer(typeof(Person));
dcs.WriteObject(w, p);
}
}
}
Console.WriteLine(File.ReadAllText(fileName));
using (FileStream fs = File.OpenRead(fileName))
{
XmlReaderSettings rs = new XmlReaderSettings
{
ConformanceLevel = ConformanceLevel.Fragment,
};
XmlReader r = XmlReader.Create(fs, rs);
while (!r.EOF)
{
Person p = new DataContractSerializer(typeof(Person)).ReadObject(r) as Person;
Console.WriteLine(p);
}
}
File.Delete(fileName);
}
}
关于C# 从文件序列化数据契约(Contract),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7760551/
我目前正在阅读以了解有关按契约(Contract)设计/代码契约(Contract)的更多信息。 据我所知,就是写契约(Invariants,Pre and Post conditions)来确保代码
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
我正在处理 spring cloud contract 并且有一个用例,我希望在缺少一些查询参数时响应为 BAD_REQUEST,而在所有必需查询参数都存在时响应为 OK。为了实现这一点,我有不同的契
按契约(Contract)设计的最佳实践是什么? 在大学里,我通过契约(Contract)范式学习了设计 (在OO环境中) 我们已经学习了三种解决问题的方法: 1)全面编程:涵盖其所有可能的异常(ex
从理论上讲,这似乎是避免错误的一个很好的解决方案,但为什么在实践中我们听到的很少呢? 例如,为什么我们不能在 Java 或 .net 上看到对它的更多支持? 最佳答案 我也一直在寻找这个答案。但它似乎
我读到编译器可以在编译时强制执行 dbc。它是怎么做到的? 最佳答案 据我所知,迄今为止最强大的静态DbC语言是Spec# by Microsoft Research .它使用名为 Boogie 的强
我试图让 VS2010 Ultimate with Code Contracts 生成错误而不是警告。 我有这个简单的测试程序: using System.Diagnostics.Contracts;
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
如何关闭对 Linq2Sql 代码的静态检查? 最佳答案 您可以通过使用 [ContractVerification(false)] 标记有问题的类来抑制对静态代码的检查。 如果您生成的类是部分,您可
我正在使用 WCF 制作一个应用程序的原型(prototype),我正在尝试定义一个回调与派生自另一个接口(interface)的接口(interface)签订契约(Contract)。这样做,生成的
我最近在 .Net Rocks 节目 570 ( http://devjourney.com/community/dotnet-rocks-show-570-with-kevin-hazzard/ )
我注意到微软在 .NET 4 中以一种奇怪的方式命名了他们的代码契约(Contract)相关函数。 他们在“require”和“ensure”的末尾添加“s”,所以有Contract.Requires
我对 WCF 比较陌生。但是,我需要创建一个向 Silverlight 和 AJAX 客户端应用程序公开数据的服务。为了实现这一目标,我创建了以下服务作为概念证明: [ServiceContract(
我一直在兜圈子,试图弄清楚这个问题。 我正在尝试选择已结束最近契约(Contract)但仍保留上一份有效契约(Contract)的员工。 例如,一名员工拥有多份契约(Contract)(有些可能是临时
使用合同密钥,有两个函数fetchByKey和lookupByKey,后者允许我处理否定查找。我没有看到针对合同编号执行相同操作的lookup : (Template t) => ContractId
我有一个用于特定 Assets (A、B、...)的合约的 pandas 数据框。每个契约(Contract)都有开始日期、结束日期(包括两者)和日费率(契约(Contract)不能重叠)。我想生成一
我有这个代码: using System; using System.Diagnostics.Contracts; namespace TestCodeContracts { class Pr
我在使用 Flow 时遇到问题,其中给定的已实现 type 通过要求我仅使用在 type 上声明的属性而不是来限制我的对象 API要求我声明所有 type 的属性。 我是 Flow 的新手,所以我可能
我有一个使用 WCF 与后端数据库通信的 Web 应用程序。我已经一切正常,但我想知道将我相当大的服务契约(Contract)分成几个契约(Contract)是否会更好。 就目前而言,服务契约(Con
我想编写单元测试来验证我的方法不接受无效参数。使用 Code Contract 的 Contract.Requires 调用检查参数的有效性。我为什么要测试合约?我认为我的测试是一种方法规范(实际上是
我是一名优秀的程序员,十分优秀!