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 / .NET Framework / XML / May 2008

Tip: Looking for answers? Try searching our database.

How to remove xmlns:xsd at root tag via XmlSerialization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Norbert Pürringer - 24 Apr 2008 16:09 GMT
Hello,

I use Xml serialization to serialize an object into an xml file.

My root tag is defined as following:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd",
"2.0.50727.42")]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[SerializableAttribute()]
[XmlType("Style")]
public class RPStyle
{
//...
}

That will produce following xml:

<?xml version="1.0" encoding="utf-16"?>
<Style xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"/>

You see, that there is an attribute xmlns:xsd. How can I get rid of
that xmlns attribute?

Thank you,
Norbert
Martin Honnen - 24 Apr 2008 16:17 GMT
> <Style xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"/>
>
> You see, that there is an attribute xmlns:xsd. How can I get rid of
> that xmlns attribute?

Are you sure it is xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
and not xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
That is the namespace declaration of the XMLSchema-instance namespace
and might be necessary to properly qualify xsi:type or xsi:nil
attributes during serialization.

Signature

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

Ferdinand Prantl - 23 May 2008 11:35 GMT
Use the following code to get rid of the XML declaration and the namespace
declaration attributes:

// 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.