DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Live Database Migration
  • Validate XML Request Against XML Schema in Mule 4
  • Validate JSON Request Against JSON Schema in Mule 4
  • Schema Change Management Tools: A Practical Overview

Trending

  • Scaling SRE Teams
  • The Most Valuable Code Is the Code You Should Not Write
  • Leveraging "INSERT INTO ... RETURNING": Practical Scenarios
  • Examining Use Cases for Asynchronous APIs: Webhooks and WebSockets

XMLSerializer - Removing Namespace & Schema Declarations xmlns:xsi xml:xsd

Douglas Rathbone user avatar by
Douglas Rathbone
·
Jun. 27, 12 · Tutorial
Like (0)
Save
Tweet
Share
48.9K Views

Join the DZone community and get the full member experience.

Join For Free

the xml serializer built into the .net framework is a pretty cool side-utility when working with anything that consumes xml. a few years ago i posted about how your should make your xml strongly-typed: because you can and it’s easy in which i discussed how easy and awesome it is to use the xsd.exe tool to quickly convert an xml file into a xsd and then further covnert into a serializable c# class file. the only not-so-perfect part of using the xml serializer is that it by default adds namespace and schema attributes that point at the w3c standard declarations – but it’s just as easy to remove these so your resulting xml looks perfectly like your original xml file.

example .net framework serializer output

<?xml version="1.0" encoding="utf-16"?>
<rootelement xmlns:xsi="http:=//www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">
</rootelement><span color="blue" style="">
</span>

image in the above example, the class i have serialized actually makes no reference to the schema or schema instance to apply, but the .net framework trying to be o-so-cool has decided to put in the default w3c schema elements.

notice the xmlns:xsi and xml:xsd tags?

when creating files that need to exactly match an output file (such as when you are using my previous post to replicate an xml file for serialization), this doesn’t help at all.

as with most niggling issues like this, there is an easy, simple way to remove these un-needed elements, and you won’t lose any sleep implementing them.

the fix

an example usage of an xml serializer in .net looks quite similar to the below piece of code:

xmlserializer s = new xmlserializer(typeof(t));
stringwriter mywriter = new stringwriter();
s.serialize(mywriter, myobject);

the msdn documentation for the xmlserializer shows a number of different overloads for the .serialize() method, and one of them takes a list of namespaces and applies them to the output – this is exactly what we want to use in our case, as we want to specify them to be empty – for this, we use the overload listed here .

taking the above and applying it to our goal of having no namespaces of schemas, we will send the serializer a namespace but make it empty.

this makes our solution’s code looks like this:

xmlserializernamespaces ns = new xmlserializernamespaces();
ns.add("","");
xmlserializer s = new xmlserializer(typeof(t));
stringwriter mywriter = new stringwriter();
s.serialize(mywriter, myobject, ns);

and our resulting xml looks like:

<?xml version="1.0" encoding="utf-16"?>
<rootelement>
</rootelement>

done!

Schema

Published at DZone with permission of Douglas Rathbone, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Live Database Migration
  • Validate XML Request Against XML Schema in Mule 4
  • Validate JSON Request Against JSON Schema in Mule 4
  • Schema Change Management Tools: A Practical Overview

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: