Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

XML Serialization - Remove XML-instance namespace?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Spam Catcher - 17 Mar 2008 07:03 GMT
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!

Signature

spamhoneypot@rogers.com (Do not e-mail)

kimiraikkonen - 17 Mar 2008 11:26 GMT
> Hello Everyone,
>
[quoted text clipped - 11 lines]
> --
> 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.
Martin Honnen - 17 Mar 2008 13:45 GMT
> When I'm serializing my objects to XML, .NET seems to put the following
> namespace into certain elements:
[quoted text clipped - 3 lines]
>
> Does anyone  know what its for?

XML serialization is based on W3C XSD schemas. These namespaces are the
instance namespace and the schema namespace, depending on the
serialization attributes there might be attributes like xsi:type in the
serialized markup for which the namespace declarations are necessary.

> Is there a way to prevent .NET from adding
> this information?

Why do you want top prevent them? Namespace declarations do no harm
usually. I think the only way to prevent them is to set up an XmlWriter
that suppresses these attributes and pass that writer to the Serialize
method.

Signature

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

Jarlaxle - 18 Mar 2008 15:34 GMT
you can prevent it by doing the following...

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();

you can also get rid of the version declaration.  to do this you need to
derive a class from XmlTextWriter and override the WriteStartDocument method
to either call the base.WriteStartDocument or not.  Then use that instead of
the XmltextWriter instance above.

> Hello Everyone,
>
[quoted text clipped - 8 lines]
>
> Thanks!
Spam Catcher - 18 Mar 2008 19:29 GMT
> XmlTextWriter formatter = new XmlTextWriter(stringWriter);
> XmlSerializerNamespaces namespaceSerializer= null;
[quoted text clipped - 7 lines]
> 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!

Signature

spamhoneypot@rogers.com (Do not e-mail)

Ferdinand Prantl - 23 May 2008 11:24 GMT
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);
   }
}
Ferdinand Prantl - 23 May 2008 11:36 GMT
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);
   }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.