简体   繁体   English

DataContractSerializer-名称不能以“。”开头 字符,十六进制值0x2E

[英]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: 问题特征:

  1. The error points to Line=0, Position=1 (which does not make sense) 错误指向Line = 0,Position = 1(没有意义)
  2. There is no element with name containing "." 没有名称包含“。”的元素。
  3. All classes that make it into the file are properly decorated with DataContract 使其成为文件的所有类均已使用DataContract进行了适当的装饰
  4. The XML file is checked to really be in UTF-8 encoding (when read by Notepad++) and none of the other versions of (de)serializing code listed on SO (that implicitly specify UTF-8 encoding) helped 检查XML文件是否确实采用UTF-8编码(当由Notepad ++读取时),SO上列出的(反)序列化代码的其他版本(隐式指定UTF-8编码)均无帮助
  5. I know the file is ugly (autogenerated element names like "d4p1: x002E " - Im the only dev in this company and unfortunately I dont have time to nicely decorate 100+ classes) 我知道文件很丑陋(自动生成的元素名称,例如“ d4p1: x002E ”-我是该公司唯一的开发人员,不幸的是我没有时间很好地装饰100多个类)
  6. Everything was working fine for 2.5 years, the problems started today. 在过去的2.5年中,一切正常,问题从今天开始。

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: 所以你要么:

  • Have a data member with "." 有一个带有"."的数据成员"." as its name. 作为它的名字。
  • Have implemented 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.

相关问题 名称不能以&#39;&#39;字符开头,十六进制值0x20 - Name cannot begin with the ' ' character, hexadecimal value 0x20 名称不能以 '&lt;' 字符十六进制值 0x3c 开头 - Name cannot begin with the '<' character hexadecimal value 0x3c XDocument错误名称不能以&#39;&lt;&#39;字符开头,十六进制值0x3C - XDocument Error Name cannot begin with the '<' character, hexadecimal value 0x3C 使用xslt时,名称不能以&#39;=&#39;字符十六进制值0x3d开头 - Name cannot begin with the '=' character hexadecimal value 0x3d when use xslt xml.Linq:“名称不能以 &#39;?&#39; 开头字符,十六进制值 0x3F&quot; - xml.Linq: "Name cannot begin with the '?' character, hexadecimal value 0x3F" 名称不能以“1”字符开头,十六进制值0x31。 第2行,第2位 - Name cannot begin with the '1' character, hexadecimal value 0x31. Line 2, position 2 将Firebase广告添加到Xamarin.Forms会导致:名称不能以&#39;$&#39;字符开头,十六进制值为0x24 - Adding Firebase Ads to Xamarin.Forms causes: Name cannot begin with the '$' character, hexadecimal value 0x24 名称不能以“1”字符开头,十六进制值0x31。 从xml文件中读取时 - Name cannot begin with the '1' character, hexadecimal value 0x31. while reading from an xml file WCF服务参考 - 获取“XmlException:名称不能以&#39;&lt;&#39;字符开头,十六进制值0x3C”在客户端 - WCF Service Reference - Getting “XmlException: Name cannot begin with the '<' character, hexadecimal value 0x3C” on Client Side 获取System.Xml.XmlException:名称不能以&#39;&#39;字符开头,十六进制值为0x20。 第42行,位置36 - Getting System.Xml.XmlException: Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 42, position 36
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM