0

I have been trying to serialize an object which has an enum

 public EEEEEEE HandledAirlineCopyInd { get; set; }
    [XmlIgnore]
    public bool RRRRRRR{ get; set; }
    [XmlElement("MatchInfo", typeof(TTTTTTTTT), Order = 11)]
    [XmlElement("MessageInfo", typeof(YYYYYYYYY), Order = 11)]
    public object Item { get; set; }
    [XmlAttribute]
    public XXXXXXXX ModuleID { get; set; }
    [XmlElement(Order = 5)]

The attribute ModuleId is an enum

[Serializable]
[GeneratedCode("System.Xml", "4.0.30319.34283")]
[XmlType(Namespace = "http://VVVV/common/7/0")]
public enum XXXXXXXX 
{
    WM = 0,
    WT = 1,
    WI = 2,
}

when I am trying to serialize as below,

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        XmlSerializer serializer = new XmlSerializer(objectToSerialize.GetType());
        MemoryStream ms = new MemoryStream();
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = omitDeclaration;
        settings.Encoding = encoding;
        XmlWriter writer = XmlWriter.Create(ms, settings);
        serializer.Serialize(writer, objectToSerialize, ns);
        string xmlString = encoding.GetString(ms.ToArray());
        string xmlStringCleaned = CleanInvalidXmlChars(xmlString);

It is not serializing. please advise.

Zafar
  • 147
  • 14
  • I cannot reproduce your problem with what you have shown. See https://dotnetfiddle.net/mrBn2Z. Can you add a [complete, reproducible example](https://stackoverflow.com/help/mcve) of your problem including a full class definition and construction of an instance that crashes in serialization? – dbc Jun 09 '16 at 05:55
  • [Serializable] [XmlType(Namespace = "http://xxx/common/7/0")] public enum WTR_LostPropertyRegisterRQModuleID { WM = 0, WT = 1, WI = 2, } – Zafar Jun 09 '16 at 06:32
  • [Serializable] [DesignerCategory("code")] [XmlType(AnonymousType = true, Namespace = "http://xxx/WTR_DelayedBagsCreateRQ/7/0")] [DebuggerStepThrough] public class WTR_DelayedBagsCreateRQ { [XmlIgnore] public bool HandledAirlineCopyIndSpecified { get; set; } [XmlAttribute] public WTR_LostPropertyRegisterRQModuleID ModuleID { get; set; } [XmlIgnore] public bool TracingOptionSpecified { get; set; } } – Zafar Jun 09 '16 at 06:32
  • Still not sure I see your problem - I can serialize your class without an exception getting thrown. See https://dotnetfiddle.net/JXGFSW. Can you edit your question to explain what you mean by *It is not serializing*? – dbc Jun 09 '16 at 06:40
  • In my serialized xml ModuleId is not there. I can see your example perfectly fine but in my application ModuleId is not included in the xml. – Zafar Jun 09 '16 at 06:50
  • What does `CleanInvalidXmlChars` do? Could that be causing a problem? – dbc Jun 09 '16 at 06:54
  • private static string CleanInvalidXmlChars(string text) { string re = @"[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF]"; return Regex.Replace(text, re, ""); } – Zafar Jun 09 '16 at 06:55
  • 1
    Make sure you don't have any out-of-date pre-generated XmlSerializer assemblies. For background see [Boost performance with Pre-generated XmlSerializers](https://blogs.msdn.microsoft.com/crm/2009/02/02/boost-performance-with-pre-generated-xmlserializers/) and [XML Serializer Generator Tool (Sgen.exe)](https://msdn.microsoft.com/en-us/library/bk3w6240.aspx) and maybe https://stackoverflow.com/questions/134224/generating-an-xml-serialization-assembly-as-part-of-my-build – dbc Jun 09 '16 at 07:00

0 Answers0