You can remove the namespaces by passing an empty XmlSerializerNamespaces
object to the Serialize method:

        static void Main(string[] args)
        {
                XmlSerializer oSer = new
XmlSerializer(typeof(TestClass));
                StringWriter sw = new StringWriter();
                TestClass oTC = new TestClass();
                oSer.Serialize(sw, oTC, new XmlSerializerNamespaces() );

^^^^^^^^^^^^^^^^^^^^^^^^^^
                Console.WriteLine("{0}", sw.ToString());
                Console.ReadLine();
        }

Check the archives of this list for more details on that.

I haven't found anything how to omit the document declaration, but you can
easily strip it using an XmlDocument or an XmlTextReader.

HTH,

Christoph Schittko
Software Architect
MSHOW.com - Web Conferencing and Interactive Broadcasting
----- Original Message -----
From: "Wilson Chiesa" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, May 12, 2002 2:38 PM
Subject: [DOTNET] XMLSerializer Issue


Hello,

I trying to serialize an object to use the result in other XML file. For
example:
I have the following XML File an inside the <Object/> element I want to
insert the serialization's result.

<MyXML>
        <Data_1/>
        <Data_2/>
        <Object/>
</MyXML>

The problem is that when I serialize the object I obtain as result the
following XML:

<?xml version="1.0" encoding="utf-16"?>
<Root xmlns:xsd="http://www.w3.org/2001/XMLSchema";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
  <id>1</id>
  <firstName>Pablo</firstName>
  <lastName>Fernandez</lastName>
</Root>

Therefore, I should clean the result to put it inside the first XML file
(I should take out the processing instruction and the namespaces).

I know that the processing instructions is added by design as well as
the namespaces, if I don't specify anyone. The question is, is there any

way to avoid the processing instruction and the namespaces without using
a clean function ?

Below is the code that I am using.

Thanks in advanced,
Wilson Chiesa.


Code:
-----

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

class TestApp
{
        static void Main(string[] args)
        {
                XmlSerializer oSer = new
XmlSerializer(typeof(TestClass));
                StringWriter sw = new StringWriter();
                TestClass oTC = new TestClass();
                oSer.Serialize(sw, oTC);
                Console.WriteLine("{0}", sw.ToString());
                Console.ReadLine();
        }
}


[System.Xml.Serialization.XmlRootAttribute("Root")]
public class TestClass
{
        public int id = 1;
        public string firstName = "Pablo";
        public string lastName = "Fernandez";
}

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to