How to remove xmlns:xsd at root tag via XmlSerialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Norbert_P=FCrringer?=

    How to remove xmlns:xsd at root tag via XmlSerialization

    Hello,

    I use Xml serialization to serialize an object into an xml file.

    My root tag is defined as following:

    [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
    "2.0.50727. 42")]
    [System.Componen tModel.Designer CategoryAttribu te("code")]
    [SerializableAtt ribute()]
    [XmlType("Style" )]
    public class RPStyle
    {
    //...
    }

    That will produce following xml:

    <?xml version="1.0" encoding="utf-16"?>
    <Style xmlns:xsd="http ://www.w3.org/2001/XMLSchema-instance"/>

    You see, that there is an attribute xmlns:xsd. How can I get rid of
    that xmlns attribute?

    Thank you,
    Norbert
  • Martin Honnen

    #2
    Re: How to remove xmlns:xsd at root tag via XmlSerializatio n

    Norbert Pürringer wrote:
    <Style xmlns:xsd="http ://www.w3.org/2001/XMLSchema-instance"/>
    >
    You see, that there is an attribute xmlns:xsd. How can I get rid of
    that xmlns attribute?
    Are you sure it is xmlns:xsd="http ://www.w3.org/2001/XMLSchema-instance"
    and not xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"?
    That is the namespace declaration of the XMLSchema-instance namespace
    and might be necessary to properly qualify xsi:type or xsi:nil
    attributes during serialization.


    --

    Martin Honnen --- MVP XML

    Comment

    • =?Utf-8?B?RmVyZGluYW5kIFByYW50bA==?=

      #3
      RE: How to remove xmlns:xsd at root tag via XmlSerializatio n

      Use the following code to get rid of the XML declaration and the namespace
      declaration attributes:

      // 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);
      }
      }

      Comment

      Working...