473,473 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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!

--
sp**********@rogers.com (Do not e-mail)
Mar 17 '08 #1
4 15069
On Mar 17, 8:03 am, Spam Catcher <spamhoney...@rogers.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...@rogers.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.dtd">

Just an idea.
Mar 17 '08 #2
=?Utf-8?B?SmFybGF4bGU=?= <Ja******@discussions.microsoft.comwrote in
news:72**********************************@microsof t.com:
XmlTextWriter formatter = new XmlTextWriter(stringWriter);
XmlSerializerNamespaces namespaceSerializer= null;

if (!bNamespaces)
{
namespaceSerializer = new XmlSerializerNamespaces();
namespaceSerializer.Add("", "");
}
XmlSerializer xmlSerializer= new XmlSerializer(obj.GetType());
xmlSerializer.Serialize(formatter, obj, namespaceSerializer);
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!

--
sp**********@rogers.com (Do not e-mail)
Mar 18 '08 #3
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(object source, string file)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);

using (XmlWriter writer = XmlWriter.Create(file, settings))
{
XmlSerializer serializer = new XmlSerializer(source.GetType());
serializer.Serialize(writer, source, namespaces);
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #4
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(object source, string file)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;

using (XmlWriter writer = XmlWriter.Create(file, settings))
{
XmlSerializer serializer = new XmlSerializer(source.GetType());

XmlSerializerNamespaces namespaces = new
XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);

serializer.Serialize(writer, source, namespaces);
}
}


*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Just D | last post by:
Hi, I need to write a serialization (to XML string) and restoring (from XML string) of a very complicated object. The object uses a few classes, one class has two ArrayLists, etc. The general...
7
by: Dave | last post by:
Hi all, I know there is a lot of information on here about Serialization, but I can't find what I need so I thought i would ask. I am trying to serialize and deserialize a set of data. ...
1
by: Dave | last post by:
I'm having trouble dynamically deserializing information. The following code is what I have. It serialises fine, the Dave element is serialized as the Name of the element, not its value. On...
2
by: Vinayak Kamat | last post by:
Hi, I've got two classes - Entity which will have an array of Child objects in it and Child class. class Entity { public Child Children;
1
by: Miguel Dias Moura | last post by:
Hello, Could someone tell me what are the main differences between using Binary or XML serialization? I am saving complex types in my profile but I am not sure which serialization to use. ...
4
by: jw56578 | last post by:
how would i structure a C# class to properly serialize into the following XML Schema <root> <MainItem> <SubItem/> <SubItem/> </MainItem> </root>
4
by: Sahar | last post by:
Hi there, I m trying to return an object (of my own written class) from a web service that contains jagged Arrays as public variables. Asp.Net is showing me the its serialized version on the...
5
by: =?Utf-8?B?TmF0aGFuIFdpZWdtYW4=?= | last post by:
Hi, We have an app that uses XML serialization throughout. As everyone probably knows, using XML serialization is not always a good idea in a big project...
5
by: RobinS | last post by:
I want to serialize a class that I am using to retain some information the user types into a screen. I have 3 questions. 1) I serialized it as XML to start with. This works, but how do I...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.