Serializing WITHOUT xmlns

前端 未结 4 1835
野性不改
野性不改 2020-12-17 10:30

I have a couple extension methods that handle serialization of my classes, and since it can be a time consuming process, they are created once per class, and handed out by t

相关标签:
4条回答
  • 2020-12-17 10:48

    A working solution, for the record!

    var ns = new XmlSerializerNamespaces();
    ns.Add("", ""); 
    var serializer = new XmlSerializer(yourType); 
    serializer.Serialize(xmlTextWriter, someObject, ns);
    
    0 讨论(0)
  • 2020-12-17 10:48
        public static string SerializeToXml(object obj)
        {
            UTF8Encoding encoding = new UTF8Encoding(false);
    
            var xmlAttributes = new XmlAttributes();
            xmlAttributes.Xmlns = false;
            xmlAttributes.XmlType = new XmlTypeAttribute() { Namespace = "" };
    
            var xmlAttributeOverrides = new XmlAttributeOverrides();
    
            var types = obj.GetType().Assembly.GetTypes().Where(t => string.Equals(t.Namespace, obj.GetType().Namespace, StringComparison.Ordinal));
            foreach (var t in types) xmlAttributeOverrides.Add(t, xmlAttributes);
    
            XmlSerializer sr = new XmlSerializer(obj.GetType(), xmlAttributeOverrides);
    
            MemoryStream memoryStream = new MemoryStream();
            StreamWriter writer = new StreamWriter(memoryStream, encoding);
    
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add(string.Empty, string.Empty);
    
            // get the stream from the writer
            memoryStream = writer.BaseStream as MemoryStream;
    
            sr.Serialize(writer, obj, namespaces);
    
            // apply encoding to the stream 
            return (encoding.GetString(memoryStream.ToArray()).Trim());
        }
    

    It will even work for complex object containing nested object Credits to user3444796 and Mentor

    0 讨论(0)
  • 2020-12-17 10:50

    No problem - just pass an empty string as the default namespace to the XML serializer:

    XmlSerializer newSerializer = 
       new XmlSerializer(typeOfT, "");
    

    Unfortunately, there's no easy constructor overload if you really need to define the XmlAttributeOverrides and the default namespace - so either you can skip the XmlAttributeOverrides and use that constructor I mentioned, or you need to use the one that defines all possible parameters (including XmlAttributeOverrides and default XML namespaces - and a few more).

    0 讨论(0)
  • 2020-12-17 11:00
     public static byte[] SerializeByteByType(object objectToSerialize, Type type)
        {
            XmlWriterSettings xmlSetting = new XmlWriterSettings()
            {
                NewLineOnAttributes = false,
                OmitXmlDeclaration = true,
                Indent = false,
                NewLineHandling = NewLineHandling.None,
                Encoding = Encoding.UTF8,
                NamespaceHandling = NamespaceHandling.OmitDuplicates
            };
    
            using (MemoryStream stm = new MemoryStream())
            {
                using (XmlWriter writer = XmlWriter.Create(stm, xmlSetting))
                {
                    var xmlAttributes = new XmlAttributes();
                    var xmlAttributeOverrides = new XmlAttributeOverrides();
    
                    xmlAttributes.Xmlns = false;
                    xmlAttributes.XmlType = new XmlTypeAttribute() { Namespace = "" };
                    xmlAttributeOverrides.Add(type, xmlAttributes);
    
                    XmlSerializer serializer = new XmlSerializer(type, xmlAttributeOverrides);
                    //Use the following to serialize without namespaces
                    XmlSerializerNamespaces xmlSrzNamespace = new XmlSerializerNamespaces();
                    xmlSrzNamespace.Add("", "");
    
                    serializer.Serialize(writer, objectToSerialize, xmlSrzNamespace);
                    stm.Flush();
                    stm.Position = 0;
                }
    
                return stm.ToArray();
            }
        }         
    
    0 讨论(0)
提交回复
热议问题