Serializing OPML via an OPML Object Model

As I promised in a previous post I have uploaded the OPML source code.

The code is a basic Main() which instantiates the OPML object, serializes it and then deserializes it (to kind of prove that it works).

The Object model isn't very clean, infact it is pretty hackey! But anyway it is here now :)

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using OPML.OPML;

namespace OPML
{
///
/// Summary description for Class1.
///
internal class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
private static void Main(string[] args)
{
Opml o = new Opml();
o.body = new OpmlBody();
o.body.outline = new OpmlOutline[1];
o.body.outline[0] = new OpmlOutline("Yo Yo");
o.body.outline[0].title = "Test";
o.body.outline[0].Url = "uasd";
o.body.outline[0].Type = "link";
o.body.outline[0].htmlUrl = "www.kinlan.co.uk";

XmlSerializer xs = new XmlSerializer(typeof (Opml));
StringBuilder sb = new StringBuilder();

TextWriter sr = new StringWriter(sb);

xs.Serialize(sr, o);

sr.Close();

Console.Write(sb.ToString());
Console.Read();

TextReader tr = new StringReader(sb.ToString());

Opml o2 = (Opml) xs.Deserialize(tr);

}
}

namespace OPML
{
[XmlRoot("opml")]
public class Opml
{
public OpmlHead head;
public OpmlBody body;

///
/// Initializes a new instance of the class.
///
public Opml()
{
body = new OpmlBody();
head = new OpmlHead();
}
}

[XmlRoot("body")]
public class OpmlBody
{
[XmlElement("outline")] public OpmlOutline[] outline;
}

[XmlRoot("head")]
public class OpmlHead
{
[XmlAttribute] public string title;
[XmlAttribute] public string dateCreated;
[XmlAttribute] public string dateModified;
[XmlAttribute] public string ownerName;
[XmlAttribute] public string ownerEmail;
[XmlAttribute] public string ownerId;
[XmlAttribute] public string docs;
[XmlAttribute] public string expansionState;
[XmlAttribute] public string vertScrollState;
[XmlAttribute] public string windowTop;
[XmlAttribute] public string windowLeft;
[XmlAttribute] public string windowBottom;
[XmlAttribute] public string windowRight;
}

[XmlRoot("outline")]
public class OpmlOutline
{
private string _text;
[XmlAttribute] public string title;
private string _type;
private string _url; // when type == link, this must not be null
[XmlAttribute] public string description;
[XmlAttribute] public string xmlUrl;
[XmlAttribute] public string htmlUrl;
[XmlAttribute] public string language;
[XmlElement("outline")] public OpmlOutline[] outline;

///
/// Initializes a new instance of the class.
///
public OpmlOutline()
{
//Text = inText; //Use the property so that it can check the values.
}

///
/// Initializes a new instance of the class.
///
/// The in text.
public OpmlOutline(string inText)
{
Text = inText; //Use the property so that it can check the values.
}

///
/// Gets or sets the text.
///
/// The text.
[XmlAttribute("text")]
public String Text
{
get { return _text; }
set
{
if(value == null)
{
throw new ArgumentNullException("Outline Text must not be null");
}

if(value.Length == 0)
{
throw new ArgumentException("Outline Text must not be blank");
}

_text = value;
}
}

[XmlAttribute("type")]
public String Type
{
get { return _type; }
set
{
if(value == null)
{
throw new ArgumentNullException("Type must not be null");
}

if(value.Length == 0)
{
throw new ArgumentException("Type must not be blank");
}

if(value.ToUpper() == "LINK")
{
if(Url == null)
{
throw new ArgumentException("Url must not be Null when Type=Link");
}
else if (Url.Length == 0)
{
throw new ArgumentException("Url must not be blank when Type=Link");
}
}
_type = value;
}
}

[XmlAttribute]
public string Url
{
get { return _url; }
set
{
if(_type != null)
{
if(_type.ToUpper() == "LINK" && value == null)
{
throw new ArgumentException("Url must not be Null when Type=Link");
}
else if (value.Length == 0)
{
throw new ArgumentException("Url must not be blank when Type=Link");
}
else
{
_url = value;
}
}
else
{
_url = value;
}
}
}
}
}
}

Technorati Tags
[feed], [feed], [feed], [feed], [feed], [feed], [feed], [feed]

Related Wikipedia Documents
, , , , , , , ,

My Related Documents
, , , , , , ,

Related Amazon Books
Service-oriented Architecture: A Field Guide to Integrating XML and Web Services: / Eric Meyer on CSS: / Professional ASP.NET 2.0: / CLR Via C#: Applied .NET Framework 2.0 Programming: / Programming C#: / The Object Primer: Agile Model-Driven Development with UML 2.0: / Model Driven Architecture: Applying MDA to Enterprise Computing: / Executable UML: A Foundation for Model Driven Architecture: / Object-oriented System Analysis: A Model Driven Approach: /

Related Images From Flickr
[[posterous-content:InwwwEJqjfcGAnweoaDm]][[posterous-content:gzbBdvBDDCFcyxwujGcu]][[posterous-content:fomyGgktanIDDDmqhsqB]][[posterous-content:tjBaoIDtwHlpaItAzCza]][[posterous-content:upCrFmrAtcBJyxmyJeof]][[posterous-content:zkvmfiBtwHxhyjvJwinB]][[posterous-content:pgGlfrDvjmtkqxllaibi]]