Tag: 序列化

序列化中基类字段的自定义XML元素名称

如何在进行序列化时更改从基类inheritance的字段的XML元素名称? 例如,我有下一个基类: public class One { public int OneField; } 序列化代码: static void Main() { One test = new One { OneField = 1 }; var serializer = new XmlSerializer(typeof (One)); TextWriter writer = new StreamWriter(“Output.xml”); serializer.Serialize(writer, test); writer.Close(); } 我得到了我需要的东西: 1 现在我创建了Ainheritance自A新类,并为其添加了字段和自定义XML元素名称: public class Two : One { [XmlElement(“SecondField”)] public int TwoField; } 序列化代码: […]

为什么XmlSerializer的Deserialize()吐出一个子对象,这是一个XmlNode ?

我正在使用XmlSerializer序列化然后反序列化一个简单的对象。 当我反对反序列化对象时,我发现一个子对象没有被正确地反序列化,而是变成了XmlNode[] 。 这里几乎是我得到的结构: // This line I put in here as a way of sneaking into the XML the // root node’s C# namespace, since it’s not the same as the // deserializing code and the deserializing code seemed unable to // deserialize properly without knowing the Type (see my code below). // So […]

Newtonsoft.json序列化和反序列化base / inheirited类来自共享项目

所以我有两个类,如下所示。 它们都在同一个命名空间和同一个共享项目中。 public class Person{ public string Name{get;set;} } public class EmployedPerson : Person{ public string JobTitle{get;set;} } 当我将这些项目serilized到rabbitmq时,我将序列化为基类,如下所示: JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple, TypeNameHandling = TypeNameHandling.Objects }; JsonConvert.SerializeObject(input, settings) 但是,当反序列化时,我会遇到问题。 我希望能够执行如下所示的操作,我将反序列化为基类,然后检查它是否是一个非常类型。 类型检查: Person person = Deserialize(e.Body, Encoding.Unicode); if (person is EmployedPerson) { logger.LogInformation(“This person has a job!”); } 反序列化设置: JsonSerializerSettings […]

XmlSerializer:删除xmlns:xsd但保留xmlns:xsi

我正在使用此方法生成XML: using (MemoryStream msRes = new MemoryStream()) using (StreamWriter objStreamWriter = new StreamWriter(msRes)) using (XmlWriter xw = XmlWriter.Create(objStreamWriter, new XmlWriterSettings() { Indent = true, IndentChars = String.Empty })) { XmlSerializer serializer = new XmlSerializer(doc.GetType()); serializer.Serialize(xw, doc); return msRes.ToArray(); } 结果是我有这样的行 。 我想删除属性xmlns:xsd=”http://www.w3.org/2001/XMLSchema”但保留xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”urn:iso:std:iso:20022:tech:xsd:camt.054.001.02″ 。 我怎样才能做到这一点 ? 谢谢你的帮助 !

按特定顺序将并行数组序列化为C#中的XML

我目前的任务是将数据发送到具有指定列表的奇怪方式的Web服务。 我无法控制架构,我尝试让另一方更改架构失败了。 所以我几乎坚持这一点。 它们的模式定义方式是这样的(仅包含相关位): 我使用xsd.exe生成一个C#类来轻松地序列化结构。 生成的位如下: [System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”, “4.0.30319.33440”)] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace=”the namespace”)] public partial class PartList { [System.Xml.Serialization.XmlElementAttribute(“Name”, Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string[] Name { get; set; } [System.Xml.Serialization.XmlElementAttribute(“Data”, Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string[] Data { get; set; } [System.Xml.Serialization.XmlElementAttribute(“OtherData”, Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string[] OtherData { get; set; } } 是的,并行数组。 现在,根据他们的文档(以及来自通过其他方式生成XML的另一方的轶事数据),正确/预期的xml应该如下所示(例如,包含两个项目的列表 – 为了说明目的添加注释内联): Some Test 123131313 0.11 […]

使用C#将列表 xml文件的属性序列化和反序列化

如何使用C#序列化和反序列化xml文件。 我为这个xml创建了可序列化的类。 在下面的一些代码中反序列化这个xml,列表只能得到单个值。 ff gg jj [Serializable, XmlRoot(“Configuration”), XmlType(“Configuration”)] public class Configuration { public Configuration() { CSVFile = new List(); } [XmlElement(“CSVFile”)] public List CSVFile { get; set; } } public class Mytutorial { string configFilePath = “above xml file path” XmlSerializer serializer = new XmlSerializer(typeof(Configuration)); FileStream xmlFile = new FileStream(configFilePath, FileMode.Open); Configuration con = […]

使用设计模式在C#中为类添加function

我的挑战:添加从/向XML序列化和反序列化到只知道如何使用JSON执行此操作的类的function。 我是C#的新手,虽然我熟悉设计模式的“基本”应用程序。 一位同事提出以下建议: 我的建议是你注入一个序列化程序(编写一个IRDCSerizliazer接口,它是一个装饰器/适配器,它包装它所构建的串行器)。 他们在这里的“开放/封闭”原则上非常重要,这可能是为什么建议使用接口和模式而不是在类中添加任何代码。 我刚刚在C#中构建了一个“装饰器”样本模式,以确保我理解如何进行“标准”装饰模式,并且感觉这种情况很复杂……(尽管我很乐意纠正) 我想我需要一些抽象“Serializer”的东西,并允许从我目前只知道JSON的工人类调用Serializer.serialize()和Serializer.deserialize()。我也认为我希望能够拥有JSON代码或“Serializer”中的XML代码基于它的构造方式.IE我从同事的提示中假设他构建了这样的模式…… 我可以像他们所说的那样“在我的舌头上”感受到这一点,但是我很难实现这一目标。 “纯粹的”装饰者模式似乎过于复杂。 帮助将不胜感激。 请不要激动我应该如何在办公室寻求更多帮助 – 显然我不想这样做,或者我不会在这里寻求帮助。 我的工作者类在下面 – 你会看到它目前只是直接调用JSON序列化和反序列化……显然这将不得不改变。 不要过分担心它的作用 – 这是一个棘手的reflection内容 – 关键是使用“注入接口”这个概念,可以使用XML和JSON在这个类中进行序列化和反序列化。 namespace XXXXXXXXXXXX { public interface IRecordSearchClientAdapter { string CallCorrectMethod(ClientServiceTypes component, string method, string request, Type[] paramTypes); } public class RecordSearchClientAdapter : IRecordSearchClientAdapter { private IDictionary componentMappings; public static IRecordSearchClientAdapter Create(IDictionary clients) { return new […]

soap 1.1的服务堆栈序列化例外

请求消息: String String String String 响应消息/错误: SerializationException Could not deserialize ‘application/xml’ request using EServices_Response.SendGetAccountNotification’ Error: System.Runtime.Serialization.SerializationException: Error in line 1 position 170. Expecting element ‘SendGetAccountNotification’ from namespace ‘mynamespace’.. Encountered ‘Element’ with name ‘Envelope’, namespace ‘http://schemas.xmlsoap.org/soap/envelope/’. at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) at ServiceStack.WebHost.Endpoints.Support.EndpointHandlerBase.DeserializeContentType(Type operationType, IHttpRequest httpReq, String […]

示例:Mono的DataContractJsonSerializer不会序列化,但.NET会

这是一个准备在Xamarin和VS2013中运行的程序。 我有一个问题,单声道不调用序列化器子类,并需要解决该问题。 题 我应该如何修改SetMembershipProof,以便它将调用位于嵌套子类中的属性[OnSerializing]的方法? using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Text; using System.Threading.Tasks; namespace MonoBug { class Program { static void Main(string[] args) { SetMembershipProof2 setMembershipProof = new SetMembershipProof2(); string setProofJSON = CryptoSerializer.Serialize(setMembershipProof); // Inspect the contents of SetProofJSON, it is null under mono, and not null in […]

使用SerializableDynamicObject进行动态排序

我需要根据运行时确定的标准对这些集合进行排序。 我使用本文中的代码来执行排序 – 最初我的代码使用了动态类。 然后我遇到了通过WCF进行序列化的问题,所以我切换到使用SerializableDynamicObject ,现在排序代码中断了: PropertyInfo pi = type.GetProperty(prop); 具有Seri​​alizableDynamicObject没有名为“Name”的属性的错误 – 其中“Name”是prop的值。 我想最简单的方法是找到一种序列化排序算法的动态类型的替代方法。 任何指向这个方向将不胜感激! 我看过这个例子,但是收到错误信息: The constructor with parameters (SerializationInfo, StreamingContext) is not found in ISerializable type