How to add attributes for C# XML Serialization

Where do you have the type stored? Normally you could have something like: class Document { [XmlAttribute(“type”)] public string Type { get; set; } [XmlText] public string Name { get; set; } } public class _Filter { [XmlElement(“Times”)] public _Times Times; [XmlElement(“Document”)] public Document Document; }

Serializing a Nullable in to XML

If you just want it to work, then perhaps: using System; using System.ComponentModel; using System.Xml.Serialization; public class Account { // your main property; TODO: your version [XmlIgnore] public Nullable<DateTime> AccountExpirationDate {get;set;} // this is a shim property that we use to provide the serialization [XmlAttribute(“AccountExpirationDate”)] [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public DateTime AccountExpirationDateSerialized { get {return AccountExpirationDate.Value;} set … Read more

DataContractSerializer – how can I output the xml to a string (as opposed to a file)

Something like this – put your output into a MemoryStream and then read that back in: public static string DataContractSerializeObject<T>(T objectToSerialize) { using(MemoryStream memStm = new MemoryStream()) { var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(memStm, objectToSerialize); memStm.Seek(0, SeekOrigin.Begin); using(var streamReader = new StreamReader(memStm)) { string result = streamReader.ReadToEnd(); return result; } } }

Force XML serialization to serialize readonly property

Honestly this doesn’t seem too bad to me as long as it is documented You should probably throw an exception if the setter is actually called: /// <summary> /// Blah blah blah. /// </summary> /// <exception cref=”NotSupportedException”>Any use of the setter for this property.</exception> /// <remarks> /// This property is read only and should not … Read more

Most elegant XML serialization of Color structure

Here’s something I’m using for serializing the Color struct in XML. It’s better than shadowing the primary Color property in my opinion. Any suggestions welcome. The XmlColor class relies primarily on the implicit operator language feature to provide the key data tranformations. Without this, the class is basically useless. Other bits of functionality were added … Read more

Java Serialization vs JSON vs XML

In general the important question is which client will receive the serialized objects – browsers/JavaScript engines like (node-js), Java client, unknown/multiple clients. JSON – JSON syntax is basically JavaScript and therefore any component with a JS engine will handle its parsing very well – even complicated data-structures will be converted to “living” objects efficiently. JSON … Read more

How to keep XmlSerializer from killing NewLines in Strings?

It is not the XmlSerializer but the XmlWriter which is removing your CR. To retain it we must have the writer convert CR to its character entity &#13;. XmlWriterSettings ws = new XmlWriterSettings(); ws.NewLineHandling = NewLineHandling.Entitize; XmlSerializer ser = new XmlSerializer( typeof( Abc ) ); using (XmlWriter wr = XmlWriter.Create( “abc.xml”, ws )) { ser.Serialize( … Read more

XmlSerializer won’t serialize IEnumerable

The way you serialize an IEnumerable property is with a surrogate property [XmlRoot] public class Entity { [XmlIgnore] public IEnumerable<Foo> Foo { get; set; } [XmlElement, Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public List<Foo> FooSurrogate { get { return Foo.ToList(); } set { Foo = value; } } } It’s ugly, but it gets the job done. The nicer … Read more

Generating XML file using XSD file

Suppose we have Test.xsd file that looks like this: <?xml version=”1.0″?> <xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”> <xs:element name=”MyClass”> <xs:complexType> <xs:sequence> <xs:element name=”Field1″ type=”xs:string”/> <xs:element name=”Field2″ type=”xs:string”/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> Create classes using xsd tool: xsd.exe /classes Test.xsd This will generate Test.cs file. Add Test.cs file to your solution. Create instance of MyClass, defined in XSD schema and … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)