469,265 Members | 1,951 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,265 developers. It's quick & easy.

How to: Remove xml header and all default namespaces

Hi
I am writing some integration project. I need to create XML from object data
but I don't need xml header and all the default namespaces.
My current (testing) code is:
Request request = new RequestGroup();

XmlSerializer serializer = new XmlSerializer(typeof(RequestGroup));
MemoryStream stream =new MemoryStream();

StreamWriter writer = new StreamWriter(stream);

serializer.Serialize(writer,requestGroup);

stream.Position = 0;

byte[] data=new byte[stream.Length];

stream.Read(data, 0, (int)stream.Length);

String result = new string(Encoding.Default.GetChars(data));

textXml.Text = result;

Thanks,

Shimon
May 8 '07 #1
2 25441
Shimon Sim wrote:
I am writing some integration project. I need to create XML from object data
but I don't need xml header and all the default namespaces.
My current (testing) code is:
Request request = new RequestGroup();

XmlSerializer serializer = new XmlSerializer(typeof(RequestGroup));
If "xml header" means the XML declaration (e.g. <?xml version="1.0"?>)
then you can get rid of it by using an XmlWriter with XmlWriterSettings
where OmitXmlDeclaration is being set to true e.g. C# pseudo code

XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter,
writerSettings))
{
serializer.Serialize(xmlWriter, request);
}
textXml.Text = stringWriter.ToString();
I guess with "all the default namespaces" you are referring to the
xmlns:xsi and xmlns:xsd namespace declarations the serializer adds to
the root element. I don't think there is a setting to get rid of them so
more effort than above is needed, for instance a custom XmlWriter
implementation that supresses the writing of the xmlns attributes.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 8 '07 #2
Thanks for your answer.
Shimon
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:uc**************@TK2MSFTNGP04.phx.gbl...
Shimon Sim wrote:
>I am writing some integration project. I need to create XML from object
data but I don't need xml header and all the default namespaces.
My current (testing) code is:
Request request = new RequestGroup();

XmlSerializer serializer = new XmlSerializer(typeof(RequestGroup));

If "xml header" means the XML declaration (e.g. <?xml version="1.0"?>)
then you can get rid of it by using an XmlWriter with XmlWriterSettings
where OmitXmlDeclaration is being set to true e.g. C# pseudo code

XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter,
writerSettings))
{
serializer.Serialize(xmlWriter, request);
}
textXml.Text = stringWriter.ToString();
I guess with "all the default namespaces" you are referring to the
xmlns:xsi and xmlns:xsd namespace declarations the serializer adds to the
root element. I don't think there is a setting to get rid of them so more
effort than above is needed, for instance a custom XmlWriter
implementation that supresses the writing of the xmlns attributes.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

May 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Shane Spraggs | last post: by
reply views Thread by Stewart at Imagine Interactive com au | last post: by
2 posts views Thread by Mario Krsnic | last post: by
5 posts views Thread by RyanL | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.