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 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
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Quick Tip: Debug XmlSerializer Errors

Quick Tip: Debug XmlSerializer Errors

Serialization and deserialization can be a bit of a black box. We look at how to find out what's happening during the serialization/deserialization process.

Jonathan Danylko user avatar by
Jonathan Danylko
·
Jul. 04, 17 · Tutorial
Like (1)
Save
Tweet
Share
4.98K Views

Join the DZone community and get the full member experience.

Join For Free

One of the hardest things to detect is whether serialization or deserialization is working when you use XmlSerializer or a DataContractSerializer.

In my Serialization/Deserialization Series, I use the XmlSerializer and found it made life a little easier.

However, there are times when you want to use an XmlSerializer over a DataContractSerializer. Here are some of the differences:

DataContractSerializer

  • It's faster than XmlSerializer (10% to be technical about it).
  • Used specifically for WCF services to serialize/deserialize to/from JSON or XML.
  • Serializes properties and fields (IMPORTANT: if it's XML, they MUST be in alphabetical order).

XmlSerializer

  • Only used for serializing/deserializing XML.
  • Allows for customizable serializing.
  • Only public properties are serialized.

The reason I bring these differences up is that XmlSerializer is a little more customizable, allowing you more flexibility when serializing or deserializing your objects.

You can add XML attributes to your code by making a property an attribute or element, whereas DataContractSerializer is a little bit harder to control.

Opening the Black Box

Way back in 2005, there wasn't a DataContractSerializer, so for our specific project, we had to use an XmlSerializer.

I recently experienced difficulties understanding why some objects in my code weren't deserializing and forgot how to check to see what was happening under the hood.

The XmlSerializer class has events detailing the deserialization process as it reads your XML.

These events occur when the serializer can't determine how to deserialize the object based on the XML you feed it.

Is it a problem with an Attribute, Node, Element, or Unreferenced Object?

var serializer = new XmlSerializer(typeof(Vendor));
serializer.UnknownAttribute += (sender, args) =>
{
    System.Xml.XmlAttribute attr = args.Attr;
    Console.WriteLine($"Unknown attribute {attr.Name}=\'{attr.Value}\'");
};
serializer.UnknownNode += (sender, args) =>
{
    Console.WriteLine($"Unknown Node:{args.Name}\t{args.Text}");
};
serializer.UnknownElement += 
    (sender, args) => 
        Console.WriteLine("Unknown Element:" 
            + args.Element.Name + "\t" + args.Element.InnerXml);
serializer.UnreferencedObject += 
    (sender, args) => 
        Console.WriteLine("Unreferenced Object:"
            + args.UnreferencedId + "\t" + args.UnreferencedObject.ToString());

Of course, you execute this before you perform your deserialization.

Conclusion

The team I work with have wrestled with some XmlSerializer and DataContractSerializer issues in the past and these events provided some clues on how to better structure your XML.

I'm pretty sure there are also some legacy applications out there still using XmlSerializers, so I thought this would be a great quick tip on debugging XML through an XmlSerializer.

How do you debug your XmlSerializer errors? Do you still use XmlSerializer? Are you using DataContractSerializer? Post your tips below and let's discuss.

Debug (command) XML

Published at DZone with permission of Jonathan Danylko, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Handle Secrets in Docker
  • Fargate vs. Lambda: The Battle of the Future
  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Reliability Is Slowing You Down

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
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: