How to rename XML attribute that generated after serializing List of objects

The most reliable way is to declare an outermost DTO class: [XmlRoot(“myOuterElement”)] public class MyOuterMessage { [XmlElement(“item”)] public List<TestObject> Items {get;set;} } and serialize that (i.e. put your list into another object). You can avoid a wrapper class, but I wouldn’t: class Program { static void Main() { XmlSerializer ser = new XmlSerializer(typeof(List<Foo>), new XmlRootAttribute(“Flibble”)); … Read more

XML Serialization question – How to Serialize Element, Attribute and Text from One Object

[XmlText], like so: using System; using System.Xml.Serialization; [Serializable, XmlRoot(“myElement”)] public class MyType { [XmlAttribute(“name”)] public string Name {get;set;} [XmlText] public string Text {get;set;} } static class Program { static void Main() { new XmlSerializer(typeof(MyType)).Serialize(Console.Out, new MyType { Name = “foo”, Text = “bar” }); } }

Ignore a property during xml serialization but not during deserialization

This is the solution outlined by Manoj: If you want to suppress serialization of a specific property Foo, but still be able to deserialize it, you can add a method public bool ShouldSerializeFoo() that always returns false. Example: public class Circle2 { public double Diameter { get; set; } public double Radius { get { … Read more

Convert Dataset to XML

You can use ds.WriteXml, but that will require you to have a Stream to put the output into. If you want the output in a string, try this extension method: public static class Extensions { public static string ToXml(this DataSet ds) { using (var memoryStream = new MemoryStream()) { using (TextWriter streamWriter = new StreamWriter(memoryStream)) … Read more

How can I XML Serialize a DateTimeOffset Property?

It’s a few years late, but here’s the quick and easy way to completely serialize DateTimeOffset using ISO 8601: [XmlElement(“lastUpdatedTime”)] public string lastUpdatedTimeForXml // format: 2011-11-11T15:05:46.4733406+01:00 { get { return lastUpdatedTime.ToString(“o”); } // o = yyyy-MM-ddTHH:mm:ss.fffffffzzz set { lastUpdatedTime = DateTimeOffset.Parse(value); } } [XmlIgnore] public DateTimeOffset lastUpdatedTime;

Is it possible to customize the namespace prefix that JAXB uses when marshalling to a String?

http://hwellmann.blogspot.com/2011/03/jaxb-marshalling-with-custom-namespace.html This shows how to do it. Another: http://www.systemmobile.com/?p=280 Key bits in case that link dies too: the NamespacePrefixMapper class, found in the com.sun.xml.bind.marshaller package. The abstract class has one method to implement: public abstract String getPreferredPrefix( String namespaceUri, String suggestion, boolean requirePrefix); then Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(”com.sun.xml.bind.namespacePrefixMapper”, new MyNamespacePrefixMapper()); If you’re also using … Read more

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; }

using XmlArrayItem attribute without XmlArray on Serializable C# class

The following should serialize properly the way you want. The clue being [XmlElement(“credentials”)] on the list. I did this by taking your xml, generating a schema (xsd) from it in Visual Studio. Then running xsd.exe on the schema to generate a class. (And some small edits) public class CredentialsSection { public string Username { get; … Read more

Suppress Null Value Types from Being Emitted by XmlSerializer

Try adding: public bool ShouldSerializeAmount() { return Amount != null; } There are a number of patterns recognised by parts of the framework. For info, XmlSerializer also looks for public bool AmountSpecified {get;set;}. Full example (also switching to decimal): using System; using System.Xml.Serialization; public class Data { public decimal? Amount { get; set; } public … Read more

tech