xmlserializer

Including xml attribute when the value is the same as the default

烂漫一生 提交于 2019-12-11 17:28:42
问题 i have an xsd we use with the XMLSerializer class in .net to generte an xml document. We have the following type with a default value <xs:complexType name="telephoneType"> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="type" default="BH"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="BH"/> <xs:enumeration value="AH"/> <xs:enumeration value="mobile"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:extension> </xs:simpleContent> It

How to keep parent element reference while parsing list of children in XML to Object in c#

微笑、不失礼 提交于 2019-12-11 15:33:59
问题 Want to keep trak of parent node in child object so that at any point I can go to immidiate parent and access its attributes and also can traverse siblings. XML: <Parent Name"P1"> <Child Name="C1"> <GChild Name="GC1"/> <GChild Name="GC2"/> </Child> <Child Name="C2"> <GChild Name="GC3"/> <GChild Name="GC4"/> </Child> </Parent> Class: [Serializable] public class Parent { [XmlAttribute] public string Name { get; set; } [XmlElement(ElementName = "Child")] public List<Child> Children { get; set; }

Deserialize XML element with xsi:nil=“true” in C#

别说谁变了你拦得住时间么 提交于 2019-12-11 13:27:06
问题 I'm trying to deserialize a XML file with XmlSerializer in C#. The destination class that follows was automatically generated using the xsd utility. [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] [System.Xml.Serialization.XmlRootAttribute(Namespace = "",

Creating XmlSerializer that serializes/deserializes derived types correctly

浪尽此生 提交于 2019-12-11 10:49:24
问题 I'm trying to create an XmlSerializer that serialize and deserializes derived types properly. Please take a look at the code below. Any assistance in using XmlAttributeOverrides ad extra types to create proper XmlSerializer and serialize an instance of GetVehicleResponse with VehicleObject as "SUV" object is greatly appreciated. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; using InteractiveSoftworks.Framework.Xml; using

How to prevent XmlSerializer from escaping < and > characters

我只是一个虾纸丫 提交于 2019-12-11 09:48:44
问题 I use an XlmSerializer to serialize a dotnet object. One property of the dotnet object is a string with this value: "<![CDATA[<p>No Comments</p>]]>" Once serialized to a StringWriter, all the < and > characters are converted to < and > including the CDATA's. How could I stop that from happening ? 回答1: Don't put the CDATA in - that's the serializer's job. You've just told the serializer to make a valid XML out of the CDATA string. It does exactly that - after deserialization, you're still left

JSON.NET, XmlSerializer and “Specified” property

元气小坏坏 提交于 2019-12-11 07:55:59
问题 I have a REST service which takes JSON and XML as input and does a SOAP call to an extenal service with the deserialized content. The classes which are used for deserialization are auto-generated from the wsdl of the SOAP service. I use the XmlSerializer in case of a XML request and I want to use the Newton JSON.NET JsonSerializer for JSON. Now I have the problem, that the WSDL generated classes contain the "Specified" property for optional atomar values (such as bool, int etc.). This is

Exclude root node from xml using xmlserializer

不想你离开。 提交于 2019-12-11 06:44:29
问题 I have a nested class which i use xmlserializer to convert it to xml. public class RequestModel{ [XmlElement("message", Namespace = "http://www.origostandards.com/schema/mtg/v2")] public Message message { get; set; } public RequestModel() { this.message = new Message(); } public class Message { //other constructor here etc } } When it serializes it all compiles without issue however the output is as follows: <RequestModel> <mtg:message> ... </mtg:message> </RequestModel> Is there a way to

C# XmlSerializer - Generic custom adapter to handle not valid AllXsd value (like java's XmlJavaTypeAdapter)

為{幸葍}努か 提交于 2019-12-11 06:37:15
问题 I deserialize an xml using a lot of serializable objects. I have read that in order to avoid not valid AllXsd value in DateTime, I have to create a helper string property which will handle the input as a string and convert it. i.e. [XmlIgnore] public DateTime? DateUpdated { get; set; } [XmlElement("updateDate")] public string DateUpdatedAsText { set { if (!string.IsNullOrWhiteSpace(value)) { try { DateUpdated = DateTime.Parse(value, CultureInfo.InvariantCulture); } catch (Exception) { } } }

Deserializing collection of types implementing IXmlSerializable runs forever

感情迁移 提交于 2019-12-11 06:31:28
问题 I have a class implementing IXmlSerializable. This class contains a few properties. Serializing and Deserializing a single instance of the class works fine. But in case of collection of the class, Serialization works fine but Deserialization runs forever. Here is a code snippet. I am using .Net 4.6.2. public class MyClass : IXmlSerializable { public int A { get; set; } public int B { get; set; } public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { this.A =

Hiding properties for the C# XmlSerializer through partial classes

自闭症网瘾萝莉.ら 提交于 2019-12-11 06:19:09
问题 According to W3C the XSD boolean datatype is defined as 0, 1, true or false. An external party has provided an xsd I have no influence over and as you might have guessed the boolean values they require are not the default values that the C# XmlSerializer serializes booleans into. True and false are not accepted, only 0 and 1. What makes the case even worse, the classes that are serialized into xml are auto generated by xsd.exe and as such I do not want to alter these classes because