我有下面的代碼片段寫在.NET的核心:異常反序列化XML wtih的DataContractSerializer在.NET核心
using System;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
namespace BugTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var xml = @"<PingDataConfirmation xmlns=""http://mycompany.com/MyProduct/Operations"">
<SourceGuid>88c02aba-f78c-4749-8665-0caddee08b8a</SourceGuid>
<Guid>88c02aba-f78c-4749-8665-0caddee08b8a</Guid>
<CreationTime>0001-01-01T00:00:00</CreationTime>
</PingDataConfirmation>";
const int buffSize = 1000000;
int bytes = 0;
int chars = 0;
byte[] result = new byte[buffSize];
bool compl;
Encoding.UTF8.GetEncoder().Convert(xml.ToCharArray(), 0, xml.Length, result, 0, buffSize, true, out chars, out bytes, out compl);
var xmlReader = XmlDictionaryReader.CreateTextReader(result, 0, bytes, new XmlDictionaryReaderQuotas());
var sr = new DataContractSerializer(typeof(PingDataConfirmation));
var deserialized = sr.ReadObject(xmlReader);
}
}
[DataContract(Namespace = "http://mycompany.com/MyProduct/Operations")]
public class PingDataConfirmation
{
[DataMember(IsRequired = true)]
public string SourceGuid { get; set; }
[DataMember(IsRequired = true)]
public string Guid { get; set; }
[DataMember(IsRequired = true)]
public DateTime CreationTime { get; set; }
}
}
它拋出異常:
System.Runtime.Serialization.SerializationException:錯誤在行1 位置86.來自命名空間 'http://mycompany.com/MyProduct/Operations'的元素'SourceGuid'不是預期的。期待 元素'CreationTime'
所以看起來,DataContractSerialized已經錯過了其中一個屬性。
那麼,DataContractSerializer的.net核心實現可能是錯誤嗎?有沒有可能調試內部,看看它爲什麼會發生?
的csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.ServiceModel.Primitives" Version="*" />
<PackageReference Include="System.ServiceModel.Http" Version="*" />
</ItemGroup>
</Project>
.NET的核心:
1.0.4