XML Serialization - Remove XML-instance namespace?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Spam Catcher

    XML Serialization - Remove XML-instance namespace?

    Hello Everyone,

    When I'm serializing my objects to XML, .NET seems to put the following
    namespace into certain elements:

    xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http ://www.w3.org/2001/XMLSchema"

    Does anyone know what its for? Is there a way to prevent .NET from adding
    this information?

    Thanks!

    --
    spamhoneypot@ro gers.com (Do not e-mail)
  • kimiraikkonen

    #2
    Re: XML Serialization - Remove XML-instance namespace?

    On Mar 17, 8:03 am, Spam Catcher <spamhoney...@r ogers.comwrote:
    Hello Everyone,
    >
    When I'm serializing my objects to XML, .NET seems to put the following
    namespace into certain elements:
    >
    xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
    >
    Does anyone know what its for? Is there a way to prevent .NET from adding
    this information?
    >
    Thanks!
    >
    --
    spamhoney...@ro gers.com (Do not e-mail)
    Withoug being sure, my guess is that the XML will take some standard
    references from W3 just like being in HTML in that code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

    Just an idea.

    Comment

    • Spam Catcher

      #3
      RE: XML Serialization - Remove XML-instance namespace?

      =?Utf-8?B?SmFybGF4bGU =?= <Jarlaxle@discu ssions.microsof t.comwrote in
      news:72620359-8D69-4D2D-B3F2-93485FBAD4AE@mi crosoft.com:
      XmlTextWriter formatter = new XmlTextWriter(s tringWriter);
      XmlSerializerNa mespaces namespaceSerial izer= null;
      >
      if (!bNamespaces)
      {
      namespaceSerial izer = new XmlSerializerNa mespaces();
      namespaceSerial izer.Add("", "");
      }
      XmlSerializer xmlSerializer= new XmlSerializer(o bj.GetType());
      xmlSerializer.S erialize(format ter, obj, namespaceSerial izer);
      formatter.Close ();
      I tried inheriting XMLSerializer and overriding the "Serialize"
      procedure... but it doesn't seem to execute. Do you if there is anything
      special to do when overriding XMLserializer?

      Thanks!

      --
      spamhoneypot@ro gers.com (Do not e-mail)

      Comment

      • Ferdinand Prantl

        #4
        RE: XML Serialization - Remove XML-instance namespace?

        If you want to get rid of both - XML declaration and those namespace
        attributes you can create your XmlWriter with explicit settings (the
        namespace attributes were already discussed above):

        // source: object instance to serialize
        // file: target file name to write the XML to
        void Serialize(objec t source, string file)
        {
        XmlWriterSettin gs settings = new XmlWriterSettin gs();
        settings.OmitXm lDeclaration = true;
        settings.Indent = true;

        XmlSerializerNa mespaces namespaces = new XmlSerializerNa mespaces();
        namespaces.Add( string.Empty, string.Empty);

        using (XmlWriter writer = XmlWriter.Creat e(file, settings))
        {
        XmlSerializer serializer = new XmlSerializer(s ource.GetType() );
        serializer.Seri alize(writer, source, namespaces);
        }
        }

        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Ferdinand Prantl

          #5
          RE: XML Serialization - Remove XML-instance namespace?

          Use the following code to get rid of the XML declaration and the
          namespace declaration attributes (the latter has been suggested above
          already) - explicit settings for the writer do the trick:

          // source: source object instance to serialize
          // target: target file name to write the XML to
          void Serialize(objec t source, string file)
          {
          XmlWriterSettin gs settings = new XmlWriterSettin gs();
          settings.OmitXm lDeclaration = true;
          settings.Indent = true;

          using (XmlWriter writer = XmlWriter.Creat e(file, settings))
          {
          XmlSerializer serializer = new XmlSerializer(s ource.GetType() );

          XmlSerializerNa mespaces namespaces = new
          XmlSerializerNa mespaces();
          namespaces.Add( string.Empty, string.Empty);

          serializer.Seri alize(writer, source, namespaces);
          }
          }




          *** Sent via Developersdex http://www.developersdex.com ***

          Comment

          Working...