Getting rid of some default attributes

Aug 14, 2012 at 10:44 PM
Edited Aug 15, 2012 at 8:38 AM

I have following XML generated with YAXLib : 

 

<Response xmlns:yaxlib="http://www.sinairv.com/yaxlib/">
  <Say Loop="5" Voice="man" yaxlib:realtype="Project.XML.Element.Say">Hello!</Say>
</Response>

 

My question is how to get rid of 

xmlns:yaxlib="http://www.sinairv.com/yaxlib/" and yaxlib:realtype="Project.XML.Element.Say"


because I need very strict XML result (already defined by web service) and I don't need deserialize anything back

Coordinator
Aug 15, 2012 at 3:40 PM

These attributes are generated because the field/property type is a base class while the object provided is of a child class. More clearly you are doing a polymorphic serialization. If you provide an object of the exact same type of the property/field these attributes will vanish.

Aug 29, 2012 at 6:11 AM

We are using a custom XmlWriter that we use for export from YAXLib, which can strip certain namespaces:

using System;
using System.Xml;
using System.IO;
using System.Collections.Generic;

namespace Export.Xml
{
    public class NamespaceStrippingXmlWriter: XmlWriter
    {
        private XmlWriter xmlWriter;
        private Stack<bool> attributeStack;
        private bool stripNextString;
        private string nameSpaceUrl;
        private string nameSpacePrefix;

        public NamespaceStrippingXmlWriter (Stream stream, XmlWriterSettings settings, string nameSpacePrefix, string nameSpaceUrl):
            base()
        {
            this.xmlWriter = XmlWriter.Create(stream, settings);
            this.attributeStack = new Stack<bool>();
            this.stripNextString = false;
            this.nameSpacePrefix = nameSpacePrefix;
            this.nameSpaceUrl = nameSpaceUrl;
        }

        #region implemented abstract members of System.Xml.XmlWriter
        public override void Close ()
        {
            xmlWriter.Close();
        }

        public override void Flush ()
        {
            xmlWriter.Flush();
        }

        public override string LookupPrefix (string ns)
        {
            return xmlWriter.LookupPrefix(ns);
        }

        public override void WriteBase64 (byte[] buffer, int index, int count)
        {
            xmlWriter.WriteBase64(buffer, index, count);
        }

        public override void WriteCData (string text)
        {
            xmlWriter.WriteCData(text);
        }

        public override void WriteCharEntity (char ch)
        {
            xmlWriter.WriteCharEntity(ch);
        }

        public override void WriteChars (char[] buffer, int index, int count)
        {
            xmlWriter.WriteChars(buffer, index, count);
        }

        public override void WriteComment (string text)
        {
            xmlWriter.WriteComment(text);
        }

        public override void WriteDocType (string name, string pubid, string sysid, string subset)
        {
            xmlWriter.WriteDocType(name, pubid, sysid, subset);
        }

        public override void WriteEndAttribute ()
        {
            if (attributeStack.Pop()) {
                xmlWriter.WriteEndAttribute();
            }
        }

        public override void WriteEndDocument ()
        {
            xmlWriter.WriteEndDocument();
        }

        public override void WriteEndElement ()
        {
            xmlWriter.WriteEndElement();
        }

        public override void WriteEntityRef (string name)
        {
            xmlWriter.WriteEntityRef(name);
        }

        public override void WriteFullEndElement ()
        {
            xmlWriter.WriteFullEndElement();
        }

        public override void WriteProcessingInstruction (string name, string text)
        {
            xmlWriter.WriteProcessingInstruction(name, text);
        }

        public override void WriteRaw (string data)
        {
            xmlWriter.WriteRaw(data);
        }

        public override void WriteRaw (char[] buffer, int index, int count)
        {
            xmlWriter.WriteRaw(buffer, index, count);
        }

        public override void WriteStartAttribute (string prefix, string localName, string ns)
        {
            bool write = (ns != this.nameSpaceUrl) && (localName != this.nameSpacePrefix || prefix != "xmlns");
            if (write) {
                xmlWriter.WriteStartAttribute(prefix, localName, ns);
            } else {
                stripNextString = true;
            }
            attributeStack.Push(write);
        }

        public override void WriteStartDocument ()
        {
            xmlWriter.WriteStartDocument();
        }

        public override void WriteStartDocument (bool standalone)
        {
            xmlWriter.WriteStartDocument(standalone);
        }

        public override void WriteStartElement (string prefix, string localName, string ns)
        {
            xmlWriter.WriteStartElement(prefix, localName, ns);
        }

        public override void WriteString (string text)
        {
            if (!stripNextString) {
                xmlWriter.WriteString(text);
            }
            stripNextString = false;
        }

        public override void WriteSurrogateCharEntity (char lowChar, char highChar)
        {
            xmlWriter.WriteSurrogateCharEntity(lowChar, highChar);
        }

        public override void WriteWhitespace (string ws)
        {
            xmlWriter.WriteWhitespace(ws);
        }

        public override System.Xml.WriteState WriteState {
            get {
                return xmlWriter.WriteState;
            }
        }
        #endregion
    }
}

We use it like this:

// Open the stream for the usecase that you need it.
Stream writeStream = File.Open("/path/to/your/output/file");
XmlWriterSettings settings = new XmlWriterSettings() {
  Indent = true
};

using (NamespaceStrippingXmlWriter xmlWriter = new NamespaceStrippingXmlWriter(writeStream, settings, "yaxlib", "http://www.sinairv.com/yaxlib/")) {
  serializer.Serialize(objectToSerialize, xmlWriter);
}