473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Force XmlSerializer to use explicit closing tags for zero-length strings?

Greetings,

I am using the .Net Framework XmlSerializer to serialize a class that
contains some string properties. The problem is that when a string
contains a zero-length string ("") it is serialized as:

<foo />

When what I would really like is:

<foo></foo>

Now I know the former is well-formed and all, but I really need the
explicit closing tag. The XML file will be read by another system (out
of my control) that requires the explicit closing tag. Any ideas here?
Thank you.

Regards,

Scott Ballard

Nov 12 '05 #1
1 8802
<sc***********@gmail.com> wrote in message news:11*********************@z14g2000cwz.googlegro ups.com...
The problem is that when a string contains a zero-length string ("") it is serialized as:
<foo />
When what I would really like is:
<foo></foo>


Scott,

You can control this by subclassing XmlTextWriter to inter-
cept all calls to WriteFullEndElement( ) and substitute a call
to WriteEndElement( ) instead. WriteEndElement( ) is smart
enough to know whether it must write a full end tag or not.

- - - XmlTextWriterEE.cs (complete)
using System;
using System.IO;
using System.Xml;

namespace Derek.Xml
{
/// <summary>
/// Wrapper that forces more compact empty element end
/// tags to be written whenever possible.
/// </summary>
public class XmlTextWriterEE : XmlTextWriter
{
public XmlTextWriterEE( TextWriter sink) : base( sink) {*;}
public override void WriteFullEndElement( ) {
base.WriteEndElement( );
}
}
}
- - -

In your case you would use this class in the project and wrap
the TextWriter you may already using in an XmlTextWriterEE
when calling Serialize( ),

serializer.Serialize( new XmlTextWriterEE( destTextWriter), obj);
Derek Harmon
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Integer Software | last post by:
Hi. I have a really simple set of classes that writes 2 pathnames to a xml file. I can write the default ok. Then if I change 1 pathname to a shorter one, then rewrite the xml file, the remains...
8
by: Harris Boyce | last post by:
Hello, I'm trying to use the FOR XML EXPLICIT clause with SQL Server to deserialize data from my database into a strongly-typed collection object that I will use throughout my application. I...
2
by: Chris Aitchison | last post by:
Hello, I am attempting to have a class that I have written serialize so that it can be both passed as a parameter or return value for a webservice, and also be serialized to disk using the...
11
by: Rangi Keen | last post by:
I am instantiating an XmlSerializer using the XmlSerializer(Type) constructor. This works most of the time, but sometimes I get a timeout during the process. I'm using the same type in all cases...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
4
by: BuddyWork | last post by:
Hello, When running the following line of code XmlSerializer serializer = new XmlSerializer(typeof (MyXmlTestClass)); I get the following exception message. An unhandled exception of
1
by: ben | last post by:
Is there anyway to force the XMLSerializer to use fully qualified namespaces for when serializing? I was in the midsts of implementing EnterpriseServices configurations using a file store and...
5
by: eggie5 | last post by:
I'm having trouble deserilizing an xml node that has empty tags. See XML: <ContentCampaigns> <ContentCampaign> <ad_id_source type="integer"></ad_id_source> <billing_code...
2
by: bmomalley | last post by:
Hi Folks, I need help, because I am just at a loss. I'm not super comfortable with XML, but I'm trying to fight my way through generating an RSS XML file for Google Base, and I'm stuck with a...
4
by: AAaron123 | last post by:
<body runat="server" id="MainBody"> <form id="form1" runat="server" style="background-color:green; width: 100%; height: 100%"> <br /> Table1" runat="server" Style="background-color:Yellow;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.