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

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • Creating a Web Project: Key Steps to Identify Issues

Trending

  • OTel Me Why: The Case for OpenTelemetry Beyond the Shine
  • BigList: a Scalable High-Performance List for Java
  • Data Governance Essentials: Policies and Procedures (Part 6)
  • Comprehensive Guide to Java String Formatting
  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.

By 
Jonathan Danylko user avatar
Jonathan Danylko
·
Jul. 04, 17 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
6.3K 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.

Related

  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • Creating a Web Project: Key Steps to Identify Issues

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • 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
  • [email protected]

Let's be friends: