xml-serialization

Don't Serialize, or Remove, TimeZone from Serialized DateTime objects

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-02 08:25:13
问题 The odd task that I have been given is to serialize a LARGE object using XML Serialization. This object contains multiple Nested UserDefined classes, with multiple DateTime fields. The Requirement for the DateTime data is that it must ALWAYS be displayed in the TimeZone of the user who initially created and set the data. Thus, I Cannot use UTC OR Local times because when de-serialized, they wouldn't be the same as they were. I also cannot display the values in UTC, they must be displayed in

Xmlserializer - Control Element-Attribute Pairing (revised)

廉价感情. 提交于 2020-01-01 19:40:53
问题 The XmlSerializer does everything I want with one exception. I need to pair an element with another element as an attribute of that element. I don't want to write a completely custom serialize method. Here's my class: public class Transaction { [XmlElement("ID")] public int m_id; [XmlElement("TransactionType")] public string m_transactiontype; [XmlAttribute("TransactionTypeCode")] public string m_transactiontypecode; } I instantiate and serialize as follows; Transaction tx = new Transaction()

Alternative to XML Serialization for Configuration

岁酱吖の 提交于 2020-01-01 11:45:29
问题 Currently, we use a giant configuration object that is serialized to/from XML. This has worked fine for the most part, but we are finding that in the case of power loss and application crashes, that the file could be left in a state that renders it unable to deserialize properly, effectively corrupting the configuration information. I would like to use the built-in app.config, but it doesn't seem to easily support custom classes. For example, with XML serialization, I can easily serialize a

XmlSerializer replace xsi:type to node name

僤鯓⒐⒋嵵緔 提交于 2020-01-01 09:04:08
问题 Currently XmlSerializer produces the following structure: <config> <BaseType xsi:type="DerivedType1" /> <BaseType xsi:type="DerivedType2" /> </config> Is there any way to make it put type name into node: <config> <DerivedType1 /> <DerivedType2 /> </config> ? 回答1: Well you can just override the element name with the XmlElement Attrribute e.g. [XmlElement("DerivedType1")] public BaseType : DerivedType1 {get;set;} if will still put the xsi:type in though, and generate even greater confusion...

Object XmlSerialization with protected property setters

拥有回忆 提交于 2020-01-01 04:49:05
问题 Here is my object [Serializable()] public class PersistentObject { public virtual int ID { get { return id; } protected set { id = value;} } ... } When I try to serialize this to xml, I get an error "The Property or indexer PersistentObject.ID cannot be used in this context because the set accessor is inaccessible" . If the setter doesn't exist, it works fine. I want to keep this ID as serialized without a hacktastic solution that involves an of [XmlIgnore()] on ID. I would prefer if I could

Roundtrip XML Serialization of DateTime and xsd:date?

会有一股神秘感。 提交于 2020-01-01 04:36:08
问题 OK, what am I missing here? MSDN says the following with regard to DateTimeSerializationMode: In versions 2.0 and later of the .Net Framework, with this property set to RoundtripDateTime objects are examined to determine whether they are in the local, UTC or an unspecified time zone, and are serialized in such a way that this information is preserved. This is the default behavior and is recommended for all new applications that do not communicate with older versions of the framework. However:

.NET XML Serialization and inheritance

痞子三分冷 提交于 2019-12-31 17:14:34
问题 I have structure like this: public interface A { public void method(); } public class B : A { } public class C : A { } List<A> list; List contains objects of type B and C they also have some fields that I would like to keep, can I now serialize it, deserialize back and get the proper object instances? Preferably to XML EDIT: Is there any simple way to serialize this list that contains interfaces, and then deserialize it back to B and C instances? 回答1: You may try using DataContractSerializer:

xstream flatten an object

末鹿安然 提交于 2019-12-31 05:25:36
问题 I am trying to flatten the xml output of xstream using a converter/marshaling with no luck. For example, public class A{ public B b; public int F; public String G; } public class B{ public String C; public String D; public int E; } is output as <A> <B> <C></C> <D></D> <E></E> </B> <F></F> <G></G> </A> but I need <A> <C></C> <D></D> <E></E> <F></F> <G></G> </A> is this possible? How to get rid of B? (C, D, E are uniquely named). Thanks. My attempt thus far has been ... public void marshal

Web services - XmlInclude in a derived class instead of a base class?

落花浮王杯 提交于 2019-12-31 04:41:27
问题 I am using an abstract class as a parameter in a web service call. Currently, I am including an XmlInclude of a derived class in the base class, like so: [XmlInclude(typeof(DerivedClass))] public abstract class BaseClass { } However, I'd rather not include all of the derived types in the base class. In http://www.pluralsight.com/community/blogs/craig/archive/2004/07/08/1580.aspx, the author mentions an alternative - writing the attribute above the web method instead, like so: [WebMethod]

C#: Best way to have XML element name from generic type name

落爺英雄遲暮 提交于 2019-12-31 04:37:14
问题 I want to create a xml for a generic class. One of the properties has the generic type. For this property I don't want to use the property name as its XML element name, but the name of the generic type. The class looks like this: [XmlRoot("Entity")] public class StoreItem<TEntity> where TEntity : class, new() { /// <summary> /// Gets and sets the status of the entity when storing. /// </summary> [XmlAttribute] public System.Data.Services.Client.EntityStates Status { get; set; } /// <summary>