[英]DataContractSerializer - Name cannot begin with the '.' character, hexadecimal value 0x2E
This question has been asked a lot of times on SO, but neither solution helped me. 关于SO的问题已经问了很多遍了,但是没有一个解决方案对我有帮助。
My datastruct is serialized into an XML file using dataContractSerializer. 我的datastruct使用dataContractSerializer序列化为XML文件。 The (de)serialization code is the following:
(反)序列化代码如下:
public static void serialize<T>(T xObject, string xFilePath, string xIndent = "")
{
XmlWriterSettings xSettings = ( xIndent == "" ? new XmlWriterSettings {Indent = false } : new XmlWriterSettings { Indent = true, IndentChars = xIndent } );
using (XmlWriter xStream = XmlWriter.Create(xFilePath, xSettings))
new DataContractSerializer(typeof(T)).WriteObject(xStream, xObject);
}
public static T deserialize<T>(string xFilePath)
{
using (FileStream xStream = new FileStream(xFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
DataContractSerializer xSerializer = new DataContractSerializer(typeof(T));
return (T)xSerializer.ReadObject(xStream);
}
}
A snippet of the written XML is 编写的XML的片段是
<?xml version="1.0" encoding="utf-8"?>
<PxePriceListEod xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="libTypes.salesApp">
<DateOfValidity xmlns:d2p1="libTypes">
<d2p1:_x002E_>2016-09-09T00:00:00</d2p1:_x002E_>
</DateOfValidity>
<PriceRecords>
<AbstractPxePriceRecordEod i:type="PxePriceRecordEodBal">
<Product xmlns:d4p1="libTypes">
<d4p1:Ccy>
<d4p1:_x002E_>EUR</d4p1:_x002E_>
</d4p1:Ccy>
<d4p1:Commodity>
<d4p1:_x002E_>Electricity</d4p1:_x002E_>
</d4p1:Commodity>
<d4p1:Duration>
<d4p1:_x002E_>Month</d4p1:_x002E_>
</d4p1:Duration>
<d4p1:Exchange>
<d4p1:_x002E_>Pxe</d4p1:_x002E_>
</d4p1:Exchange>
<d4p1:Period>
<d4p1:_x002E_>9</d4p1:_x002E_>
</d4p1:Period>
<d4p1:Type>
<d4p1:_x002E_>Base</d4p1:_x002E_>
</d4p1:Type>
<d4p1:Year>
<d4p1:_x002E_>2016</d4p1:_x002E_>
</d4p1:Year>
</Product>
<IsDeduced xmlns:d4p1="libTypes">
<d4p1:_x002E_>false</d4p1:_x002E_>
</IsDeduced>
<IsInterpolated xmlns:d4p1="libTypes">
<d4p1:_x002E_>false</d4p1:_x002E_>
</IsInterpolated>
<IsSynthetic xmlns:d4p1="libTypes">
<d4p1:_x002E_>false</d4p1:_x002E_>
</IsSynthetic>
<Price xmlns:d4p1="libTypes">
<d4p1:_x002E_>30.45</d4p1:_x002E_>
</Price>
<DateOfValidity xmlns:d4p1="libTypes">
<d4p1:_x002E_>2016-09-09T00:00:00</d4p1:_x002E_>
</DateOfValidity>
</AbstractPxePriceRecordEod>
... more AbstractPxePriceRecordEod elements ...
</PriceRecords>
</PxePriceListEod>
Features of the problem: 问题特征:
DataContract
DataContract
进行了适当的装饰 Any hint is much appreciated, Daniel 丹尼尔非常感谢任何提示
UPDATE 更新
I have added a minimal amount of code that reproduces the problem here . 我已经加入能重现问题的代码少量这里 。 The
application
tries to read the given class from an xml file, the classes that have the problematic dataContractNames are located in library\\+support\\+qprimitive
. application
尝试从xml文件中读取给定的类,而具有问题的dataContractNames的类位于library\\+support\\+qprimitive
。
_x002E_
is how XmlConvert.EncodeLocalName()
encodes the string "."
_x002E_
是XmlConvert.EncodeLocalName()
对字符串"."
编码的方式"."
. 。 See https://dotnetfiddle.net/phUYO3 for a demonstration.
请参阅https://dotnetfiddle.net/phUYO3进行演示。 So you either:
所以你要么:
"."
"."
的数据成员"."
as its name. IXmlSerializable
and are writing elements with this name. IXmlSerializable
并正在使用该名称编写元素。 That being said, making a data contract type with [DataMember(Name = ".")]
on one of the data members does not cause problems for me. 话虽这么说,对其中一个数据成员使用
[DataMember(Name = ".")]
创建数据协定类型对我来说不会造成问题。 Ie I can serialize and deserialize the following successfully: 即我可以成功序列化和反序列化以下内容:
[DataContract(Namespace = "libTypes.salesApp")]
public class PxePriceListEod
{
[DataMember]
public DateOfValidity DateOfValidity { get; set; }
}
// DateOfValidity
[DataContract(Namespace = "libTypes")]
public class DateOfValidity
{
[DataMember(Name = ".")]
public DateTime DateTime { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.