C# Win RT将XML反序列化为对象
我有一门课要为一位顾问提供一些细节:C# Win RT将XML反序列化为对象,c#,xml,windows-8,windows-runtime,xml-deserialization,C#,Xml,Windows 8,Windows Runtime,Xml Deserialization,我有一门课要为一位顾问提供一些细节: namespace MortgageApp_2 { [KnownType(typeof(MortgageApp_2.AdviserDetails))] [DataContractAttribute] public class AdviserDetails { [DataMember()] public string Consultant { get; set; } [DataMember()] public string C
namespace MortgageApp_2
{
[KnownType(typeof(MortgageApp_2.AdviserDetails))]
[DataContractAttribute]
public class AdviserDetails
{
[DataMember()]
public string Consultant { get; set; }
[DataMember()]
public string Company { get; set; }
[DataMember()]
public string AddressOne { get; set; }
[DataMember()]
public string AddressTwo { get; set; }
[DataMember()]
public string City { get; set; }
[DataMember()]
public string County { get; set; }
[DataMember()]
public string Postcode { get; set; }
[DataMember()]
public string Telephone { get; set; }
}
}
以下代码将AdviserDetails消毒为XML:
StorageFolder folder = ApplicationData.Current.LocalFolder;
string fileName = "AdviserDetails.xml";
CreationCollisionOption options = CreationCollisionOption.ReplaceExisting;
try
{
var file = await folder.CreateFileAsync(fileName, options);
using (var stream = await file.OpenStreamForWriteAsync())
{
var ser = new DataContractSerializer(typeof(AdviserDetails));
ser.WriteObject(stream, adDetails);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
if (Debugger.IsAttached)
Debugger.Break();
throw;
}
创建的XMl文件如下所示:
<AdviserDetails xmlns="http://schemas.datacontract.org/2004/07/MortgageApp_2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><AddressOne>242 Office Block</AddressOne><AddressTwo>32 Hill Street</AddressTwo><City>Birmingham</City><Company>Best Mortgages</Company><Consultant>Bill Jones</Consultant><County>West Midlands</County><Postcode>B1 1AB</Postcode><Telephone>05100021300</Telephone></AdviserDetails>
然而,最后一行:
var loadAdviser = (AdviserDetails)serializer.Deserialize(stream.AsInputStream().AsStreamForRead());
引发异常:
XML文档(1,2)中存在错误
有人知道怎么回事吗?我如何让我的应用程序创建一个它可以读取的XMl文件
谢谢!:) 试着这样做:
<AdviserDetails xmlns="http://schemas.datacontract.org/2004/07/MortgageApp_2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><AddressOne>242 Office Block</AddressOne><AddressTwo>32 Hill Street</AddressTwo><City>Birmingham</City><Company>Best Mortgages</Company><Consultant>Bill Jones</Consultant><County>West Midlands</County><Postcode>B1 1AB</Postcode><Telephone>05100021300</Telephone></AdviserDetails>
序列化程序:
static async private Task SaveAsync<T>()
{
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
var sessionSerializer = new DataContractSerializer(typeof(List<object>), new Type[] { typeof(T) });
sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
await sessionOutputStream.FlushAsync();
}
static async private Task RestoreAsync<T>()
{
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
if (sessionFile == null)
{
return;
}
IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
var sessionSerializer = new DataContractSerializer(typeof(List<object>), new Type[] { typeof(T) });
_data = (List<object>)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
}
static async private Task SaveAsync()
{
StorageFile sessionFile=await ApplicationData.Current.LocalFolder.CreateFileAsync(文件名,CreationCollisionOption.ReplaceExisting);
irandomaccesstream sessionRandomAccess=wait sessionFile.OpenAsync(FileAccessMode.ReadWrite);
IOOutputStream sessionOutputStream=sessionRandomAccess.GetOutputStreamAt(0);
var sessionSerializer=newdatacontractserializer(typeof(List),newtype[]{typeof(T)});
sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(),_数据);
等待sessionOutputStream.FlushAsync();
}
反序列化程序:
static async private Task SaveAsync<T>()
{
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
var sessionSerializer = new DataContractSerializer(typeof(List<object>), new Type[] { typeof(T) });
sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
await sessionOutputStream.FlushAsync();
}
static async private Task RestoreAsync<T>()
{
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
if (sessionFile == null)
{
return;
}
IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
var sessionSerializer = new DataContractSerializer(typeof(List<object>), new Type[] { typeof(T) });
_data = (List<object>)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
}
静态异步专用任务RestoreAsync()
{
StorageFile sessionFile=await ApplicationData.Current.LocalFolder.CreateFileAsync(文件名,CreationCollisionOption.OpenIfExists);
if(sessionFile==null)
{
返回;
}
IIInputStream sessionInputStream=等待sessionFile.OpenReadAsync();
var sessionSerializer=newdatacontractserializer(typeof(List),newtype[]{typeof(T)});
_数据=(列表)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
}
工作示例:最终成功,非常感谢您的链接。:)在WinRT中实现XML是一项真正的任务!!