草庐IT

$序列化

全部标签

c# - 为什么当我使用 JSON.NET 反序列化时会忽略我的默认值?

我使用JSON.NET作为我的主要序列化器。这是我的模型,看我设置了一些JSONProperties和一个DefaultValue.publicclassAssignmentContentItem{[JsonProperty("Id")]publicstringId{get;set;}[JsonProperty("Qty")][DefaultValue(1)]publicintQuantity{get;set;}}当我序列化List时,它做得很好:privatestaticJsonSerializerSettingss=newJsonSerializerSettings{Default

c# - 对象实体到 CSV 序列化/转换

如何在C#中将所有值(属性)写入csv格式的字符串?例如:classPerson(stringfirstName,stringlastName,int_age);Personperson=newPerson("Kevin","Kline",33);现在我想要一个字符串"Kevin;Kline;33"换句话说,我想将一个对象序列化为CSV 最佳答案 看看JoshClose's优秀CSVHelper图书馆varperson=newPerson("Kevin","Kline",33);using(varcsv=newCsvWriter(n

c# - 序列化时如何跳过xml声明?

我正在尝试输出一个没有xmlhead的xml文件我试过:Typet=obj.GetType();XmlSerializerxs=newXmlSerializer(t);XmlWriterxw=XmlWriter.Create(@"company.xml",newXmlWriterSettings(){OmitXmlDeclaration=true,Indent=true});xs.Serialize(xw,obj);xw.Close();但它仍然在xml文件中输出。我不想要弦乐技巧。有什么想法吗? 最佳答案 将Conformance

c# - 替换二进制文件中的字节序列

将二进制文件中的字节序列替换为与其他字节长度相同的最佳方法是什么?二进制文件会非常大,大约50MB,不应立即加载到内存中。更新:我不知道需要替换的字节的位置,我需要先找到它们。 最佳答案 假设您正在尝试替换文件的已知部分。打开一个具有读/写权限的FileStream找对地方覆盖现有数据示例代码来了...publicstaticvoidReplaceData(stringfilename,intposition,byte[]data){using(Streamstream=File.Open(filename,FileMode.Ope

c# - 将 xml 反序列化为对象时出错 : System. FormatException 输入字符串格式不正确

您好,在此先感谢您的帮助。我在尝试使用XmlSerializer和StringReader将XElement反序列化为对象时遇到问题。我的反序列化代码是这样的:/*deseierializeasingleRegisterServerObjectinstancefromxml*/staticRegisterServerObjectDeserializeSingleServerFromXml(XElementserverElement){XmlSerializerdeserializer=newXmlSerializer(typeof(RegisterServerObject));Regi

c# - 无法通过序列号找到证书

我已将我的证书导入个人->证书。我使用以下代码行通过序列号查找我的证书,但我不能:publicX509Certificate2CollectionFindCerts(stringserialNumber){varsearchType=X509FindType.FindBySerialNumber;varstoreName="MY";varcertificatesStore=newX509Store(storeName,StoreLocation.LocalMachine);certificatesStore.Open(OpenFlags.OpenExistingOnly);varmat

c# - 使用 DataContractSerializer 反序列化 XML

我有一个返回以下数据的网络服务:1234525ELMSTELMST我有一个匹配的类结构:[DataContract(Name="RESPONSE",Namespace="")]publicclassResponse{[DataMember(Name="KEY")]publicstringKey{get;set;}[DataMember(Name="PROPERTY")]publicPropertyProperty{get;set;}}[DataContract(Name="PROPERTY",Namespace="")]publicclassProperty{[DataMember(N

c# - Newtonsoft Json 将字典反序列化为 DataContractJsonSerializer 中的键/值列表

我有一个用DataContractJsonSerializer序列化到存储的字典,我想用Newtonsoft.Json反序列化它。DataContractJsonSerializer已将字典序列化为键/值对列表:{"Dict":[{"Key":"Key1","Value":"Val1"},{"Key":"Key2","Value":"Val2"}]}我可以给JsonConvert.DeserializeObject()一些很酷的选择吗?这将使它同时支持该数据格式和Newtonsoft.Json的格式?{"Dict":{"Key1":"Val1","Key2":"Val2"}}是Newt

c# - 使用 Web API 和 JSON.NET 序列化对象时防止 $id/$ref

我似乎无法阻止WebAPI/JSON.NET在序列化对象时使用Newtonsoft.Json.PreserveReferencesHandling.Objects。换句话说,尽管使用了以下设置,但$id/$ref始终在序列化对象中使用:publicclassMvcApplication:System.Web.HttpApplication{protectedvoidApplication_Start(){WebApiConfig.Register(GlobalConfiguration.Configuration);}}publicstaticclassWebApiConfig{pub

c# - 反序列化向后兼容性

我正在尝试使用旧版本的应用程序反序列化“SomeClass”。我得到以下异常System.Runtime.Serialization.SerializationException:TheObjectManagerfoundaninvalidnumberoffixups.ThisusuallyindicatesaproblemintheFormatter.当我序列化版本0.9并尝试使用版本0.8反序列化时,反序列化抛出异常。我认为OptionalField属性可以解决问题,但事实并非如此。//Version0.8[Serializable()]classFoo{Barb;}//Versi