Remove whitespaces in XML string

The easiest solution is probably using lxml, where you can set a parser option to ignore white space between elements: >>> from lxml import etree >>> parser = etree.XMLParser(remove_blank_text=True) >>> xml_str=””‘<root> >>> <head></head> >>> <content></content> >>> </root>”’ >>> elem = etree.XML(xml_str, parser=parser) >>> print etree.tostring(elem) <root><head/><content/></root> This will probably be enough for your needs, but … Read more

How do you use XMLSerialize for Enum typed properties in c#?

As per Darin Dimitrov’s answer – only extra thing I’d point out is that if you want control over how your enum fields are serialized out then you can decorate each field with the XmlEnum attribute. public enum Simple { [XmlEnum(Name=”First”)] one, [XmlEnum(Name=”Second”)] two, [XmlEnum(Name=”Third”)] three, }

Change the order of elements when serializing XML

Try decorating the properties of the bookingListclass with the XmlElementAttribute, in order to control how the objects of that class are going to be serialized to XML. Here’s an example: public class bookingList { [XmlElement(Order = 1)] public string error { get; set; } [XmlElement(Order = 2)] public int counter { get; set; } [XmlElement(ElementName … Read more

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

Convert XML to String and append to page

You haven’t told us how you go about displaying that object. XMLSerializer works on DOM nodes, so your object has to be added somewhere, for example: document.getElementById(‘SomeDiv’).appendChild(xml); and if you just want the full xml string to be displayed: var xmlText = new XMLSerializer().serializeToString(xml); var xmlTextNode = document.createTextNode(xmlText); var parentDiv = document.getElementById(‘SomeDiv’); parentDiv.appendChild(xmlTextNode);

ShouldSerialize*() vs *Specified Conditional Serialization Pattern

The intent of the {propertyName}Specified pattern is documented in XML Schema Binding Support: MinOccurs Attribute Binding Support. It was added to support an XSD schema element in which: The <element> element is involved. minOccurs is zero. The maxOccurs attribute dictates a single instance. The data type converts to a value type. In this case, xsd.exe … Read more

How do I add a attribute to a XmlArray element (XML Serialization)?

XmlArray is used to tell the xmlserializer to treat the property as array and serialize it according its parameters for the element names. [XmlArray(“FullNames”)] [XmlArrayItem(“Name”)] public string[] Names{get;set;} will give you <FullNames> <Name>Michael Jackson</Name> <Name>Paris Hilton</Name> </FullNames> In order to add an xml attribute to FullNames element, you need declare a class for it. [XmlType(“FullNames”)] … Read more