简体   繁体   中英

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.

My datastruct is serialized into an XML file using dataContractSerializer. 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 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)
  2. There is no element with name containing "."
  3. All classes that make it into the file are properly decorated with 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
  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)
  6. Everything was working fine for 2.5 years, the problems started today.

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 .

_x002E_ is how XmlConvert.EncodeLocalName() encodes the string "." . See https://dotnetfiddle.net/phUYO3 for a demonstration. So you either:

  • Have a data member with "." as its name.
  • Have implemented IXmlSerializable and are writing elements with this name.

That being said, making a data contract type with [DataMember(Name = ".")] on one of the data members does not cause problems for me. 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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