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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Freshness as a First-Class Schema Concern: Modeling Data Staleness Across 83 Data Sources
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Dynamic Tool Selection: A Portable Pattern for Agents Drowning in Tool Schemas
  • Select AI and Vector Search on a Legacy Oracle Schema: What It Actually Takes

Trending

  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • AI Agents and Frontier Models: Insights From The Neuron
  • How to Migrate From LangChainGo to Genkit Go
  • Moving 900+ GB of Prometheus Without a Cutover Weekend

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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

By 
Douglas Rathbone
·
Jun. 27, 12 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
52.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Freshness as a First-Class Schema Concern: Modeling Data Staleness Across 83 Data Sources
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Dynamic Tool Selection: A Portable Pattern for Agents Drowning in Tool Schemas
  • Select AI and Vector Search on a Legacy Oracle Schema: What It Actually Takes

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook