Geeks With Blogs
Path Notes of a Kodefu Master blog

I needed to stick a small piece of xml into a database field, sans any wordy junk that tends to inhabit xml. My requirement has length restrictions, so I wanted to keep the character count down. Basically, I needed something like this:

<candy>

<chocolate>yum</chocolate>

<caramel>creamy</caramel>

<nougat>whatisthis</nougat>

</candy>

Pretty simple, huh? My first inclination was to use XmlDocument and start adding nodes, but that tact turned out to be cumbersome and was too procedural for my taste. So, I turned to making a small data holding class so I could serialize it!

[Serializable]
[
XmlRoot("candy")]
public class
CandyData
{

string _chocolate;
string _caramel;
string _nougat;

[XmlElement("chocolate")]
public string Chocolate
{

get { return _chocolate; }
set { _chocolate = value; }

}

[XmlElement("caramel")]
public string Caramel
{

get { return _caramel; }
set { _caramel = value; }

}

 

[XmlElement("nougat")]
public string Nougat
{

get { return _nougat; }
set { _nougat = value; }

}

}

That's pretty straight-forward as well. The problem came when I serialized the class and look at what was being returned: declarations and namespaces. It turns out that special steps are required to strip out the xml declaration from the top, <?xml version="1.0" encoding="utf-16"?>, and the namespace, xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema, from the root element.

Instead of writing up a gigantic article describing every step I took to figure this out (google and msdn), how about I give out the code that does it and illustrates what's required!

 

public static class XmlNugget

{

/// <summary>
///
Serializes the specified obj into simple xml text.
/// </summary>
///
<param name="obj">The obj to serialize</param>
///
<returns>A simple xml nugget of joy.</returns>
public static string Serialize(Object obj)
{


XmlSerializer serializer = new XmlSerializer(obj.GetType());
StringBuilder sb = new StringBuilder();

//This will clear out default namespaces
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(
String.Empty, String.Empty);

//This is needed to get rid of the <?xml...>
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;

XmlWriter xmlWriter = XmlTextWriter.Create(sb, settings);
serializer.Serialize(xmlWriter, obj, ns);


return sb.ToString();

}


}

This is a simple static class that takes an object and spits out xml in a string. The xml is a simple structure... no namespacing or anything unless you defined it in your class attributes. In my opinion, this helper class is preferable to typing all that junk out everytime you need to serialize an object into xml and then read it into a string.

If you're looking for a more professional name, I recommend SimpleXmlText. Unfortunately, sugar cravings do weird things to a programmer's mind. That's what I get for switching to black coffee with no sugar.

Posted on Wednesday, June 27, 2007 5:22 PM Kodefu | Back to top


Comments on this post: Xml Nugget of Joy

# Links (6/27/2007)
Requesting Gravatar...
.NET Object Instantiation in C#. Part IV The Builder Pattern A simple example of using the System.IO.DirectoryInfo
Left by Member Blogs on Jun 27, 2007 8:29 PM

Your comment:
 (will show your gravatar)


Copyright © Chris Eargle | Powered by: GeeksWithBlogs.net