473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 26194
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shane Spraggs | last post by:
Hi, I'm using Xerces to produces XML output. No matter what I do, the first line is always the Xerces comment: <?xml version="1.0" encoding="UTF-16" standalone="no" ?> I need to get rid of...
3
by: Oisin Grehan | last post by:
Hi, I can't for the life of me get this to work properly. I've searched for examples, but none of the examples quite match my environment. Here is my XML (please treat this as immutable -- no...
0
by: Stewart at Imagine Interactive com au | last post by:
Hi All, Having a problem transitioning from VS2003 to VS2005 in C#. Probably a really simple answer. Basically when I'm creating a class library, rather than wrap every class in namespace...
0
by: irwin | last post by:
Hello, Recently I had to use the XmlSerializer object to deserialize one of the objects in a project. When I invoked Deserialize on the xml document containing the object definition, I would get...
2
by: Mario Krsnic | last post by:
Hello everybody, I have in a VWD-Project several sqldatasource- controls and I bind them during runtime with DetailsView: SqlDataSource2.SelectCommand = "select field1 from Tab1 where id= " & 4...
1
by: kamal | last post by:
when i add the datasource of combobox by default the first value is selected .. but when i use the Items.Add() then nothing is selected. how do i remove the default selection in combobox when...
5
by: RyanL | last post by:
I'm a newbie with a large number of data files in multiple directories. I want to uncompress, read, and copy the contents of each file into one master data file. The code below seems to be doing...
1
by: turnerca902 | last post by:
Hi Folks, I am working on a little project and hoping someone out there might offer me some info/insight. I have a folder containing about 50 html files where the contents look like this: ...
4
by: sasimca007 | last post by:
Hello friends, In my project, i am opening a window with javascript, in that new window i am providing a link to close that window, then there is no use of default close...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.