xml-serialization

EmitDefaultValue=false only working for strings

折月煮酒 提交于 2021-02-11 17:57:20
问题 I am trying to serialize the following c# class to XML [DataContract] public class LatLonPoint { [DataMember(IsRequired = true, Order = 1)] public float Lat { get; set; } [DataMember(IsRequired = true, Order = 2)] public float Lon { get; set; } [DataMember(EmitDefaultValue = false, Order = 3)] public DateTime? OptimalTime { get; set; } } When I serialize this class using the following code public static string GetLatLonPointXml(LatLonPoint data) { XmlSerializer xmlSerializer = new

C# XML Serialization How To Set Attribute xsi:type

一世执手 提交于 2021-02-11 15:39:28
问题 This is how my Xml should look after XML Serialization: <value xsi:type="CD" otherAttributes= "IDK"> . . . </value> Thats my C# code to it: public class Valué { [XmlAttribute(AttributeName ="xsi:type")] public string Type { get; set; } = "CD"; [XmlAttribute(attributeName: "otherAttributes")] public string OtherAttributes { get; set; } = "IDK" } Apparently the XmlSerializer can't serialize colons (:) in attributenames.... how do i fix this problem? If i remove the colon from the attributeName

C# XML Serialization How To Set Attribute xsi:type

落花浮王杯 提交于 2021-02-11 15:38:43
问题 This is how my Xml should look after XML Serialization: <value xsi:type="CD" otherAttributes= "IDK"> . . . </value> Thats my C# code to it: public class Valué { [XmlAttribute(AttributeName ="xsi:type")] public string Type { get; set; } = "CD"; [XmlAttribute(attributeName: "otherAttributes")] public string OtherAttributes { get; set; } = "IDK" } Apparently the XmlSerializer can't serialize colons (:) in attributenames.... how do i fix this problem? If i remove the colon from the attributeName

Accessing Child nodes during Xml Serialization

喜欢而已 提交于 2021-02-11 13:18:21
问题 How can I access the children of the Name element during Serialization? <Person> <Name> <First>John</First> <Middle>Adam</Middle> <Last>Smith</Last> <Madian></Madian> </Name> <Gender>M</Gender> </Person> [XmlRootAttribute("Person", IsNullable= false)] public class Person { [XmlElement(ElementName = "Name/First")] public string firstName; [XmlElement(ElementName = "Name/Middle", IsNullable = true)] public string middleName; [XmlElement(ElementName = "Name/Last")] public string lastName;

Accessing Child nodes during Xml Serialization

懵懂的女人 提交于 2021-02-11 13:18:09
问题 How can I access the children of the Name element during Serialization? <Person> <Name> <First>John</First> <Middle>Adam</Middle> <Last>Smith</Last> <Madian></Madian> </Name> <Gender>M</Gender> </Person> [XmlRootAttribute("Person", IsNullable= false)] public class Person { [XmlElement(ElementName = "Name/First")] public string firstName; [XmlElement(ElementName = "Name/Middle", IsNullable = true)] public string middleName; [XmlElement(ElementName = "Name/Last")] public string lastName;

Dynamic root element with Jackson

淺唱寂寞╮ 提交于 2021-02-11 06:44:13
问题 I'm currently working on a project that deals with elements that (for legacy reasons) must have a tag name that represents their type. Basically I have this: @JsonRootName("node") class NodeDocument { private String type; } Which outputs something like: <node type="someType"></node> But what's expected would be: <someType></someType> @JsonRootName doesn't seem to be usable on a method or attribute. Even though there is SerializationConfig.withRooName() or custom serializers, I can't seem to

Dynamic root element with Jackson

不羁岁月 提交于 2021-02-11 06:43:10
问题 I'm currently working on a project that deals with elements that (for legacy reasons) must have a tag name that represents their type. Basically I have this: @JsonRootName("node") class NodeDocument { private String type; } Which outputs something like: <node type="someType"></node> But what's expected would be: <someType></someType> @JsonRootName doesn't seem to be usable on a method or attribute. Even though there is SerializationConfig.withRooName() or custom serializers, I can't seem to

XML serialisation for class properties with additional meta data

ぐ巨炮叔叔 提交于 2021-02-10 14:22:59
问题 I have an entity as below public class Vehicle{ public int VehicleId {get;set;}; public string Make {get;set;}; public string Model{get;set;} } I wanted to serialize as below <Vehicle> <VehicleId AppliesTo="C1">1244</VehicleId> <Make AppliesTo="Common" >HXV</Make> <Model AppliesTo="C2">34-34</Model> </Vehicle> I have around 100 properties like this in Vehicle class, for each vehicle property I wanted to attach a metadata ApplieTo which will be helpful to downstream systems. AppliesTo

XML serialisation for class properties with additional meta data

☆樱花仙子☆ 提交于 2021-02-10 14:22:14
问题 I have an entity as below public class Vehicle{ public int VehicleId {get;set;}; public string Make {get;set;}; public string Model{get;set;} } I wanted to serialize as below <Vehicle> <VehicleId AppliesTo="C1">1244</VehicleId> <Make AppliesTo="Common" >HXV</Make> <Model AppliesTo="C2">34-34</Model> </Vehicle> I have around 100 properties like this in Vehicle class, for each vehicle property I wanted to attach a metadata ApplieTo which will be helpful to downstream systems. AppliesTo

Deserialize tag with body and attributes to an object

五迷三道 提交于 2021-02-05 08:17:25
问题 How can I deserialize XML like that to an object: <Root> <Element Attr="AttrValue1">BodyValue1</Element> <Element Attr="AttrValue2">BodyValue2</Element> <Element Attr="AttrValue3">BodyValue3</Element> </Root> I need the exact objects structure with appropriate attributes. I've tried: [XmlRoot("Root")] public class EventFieldsRoot { [XmlElement("Element")] public List<Element> Elements{ get; set; } } public class Element { [XmlAttribute] public string Attr { get; set; } [XmlElement("")] public