Chris Martin's Blog

A Developer and so on...

  Home  |   Contact  |   Syndication    |   Login
  7 Posts | 0 Stories | 22 Comments | 20 Trackbacks

News

Tag Cloud


Archives

Post Categories

Blogs I check out

Apparently .NET 2.0 does this out of the box but, I don’t get to play with it for awhile so….

Today I had the need to serialize some error messages in CDATA elements. Quickly, I realized that XmlSerializer doesn’t support this out of the box. After a really quick googlin’ session, I had my solution. And since I’m nice, I’ll share it with y’all.

Say you have a class called ErrorMessage that has a string to be serialized:

[Serializable]

public class ErrorMessage

{

private string message

….

[XmlElement("message")]

public string Message

{

get { return message; }

set { message = value; }

}

….

}

If you want to inject the message into a CDATA element do the following:

Create a class to hold your CDATA string and implement IXmlSerializable.

public class CDATA : IXmlSerializable

{

private string text;

public CDATA()

{}

public CDATA(string text)

{

this.text = text;

}

public string Text

{

get { return text; }

}

XmlSchema IXmlSerializable.GetSchema()

{

return null;

}

void IXmlSerializable.ReadXml(XmlReader reader)

{

this.text = reader.ReadString();

}

void IXmlSerializable.WriteXml(XmlWriter writer)

{

writer.WriteCData(this.text);

}

}

And change your original ErrorMessage class to

[Serializable]

public class ErrorMessage

{

private CDATA message

….

[XmlElement("message", Type=typeof(CDATA))]

public CDATA Message

{

get { return message; }

set { message = value; }

}

….

}

Nice and simple.

posted on Wednesday, November 30, 2005 1:51 PM

Feedback

# re: Serializing A String Within a CDATA Element (.NET 1.1) 2/27/2006 11:35 PM Simone
Chris,
how do you do with 2.0?
How is it done "out of the box"?
I cannot find it

# re: Serializing A String Within a CDATA Element (.NET 1.1) 6/22/2006 12:53 PM Craig Eddy
Wow...you might not have to go to this extent. Create a new XmlElement property and mark it with [XmlAnyElement("message")].

Then in the get use:

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.XmlElement elem = doc.CreateElement( "message" );
System.Xml.XmlCDataSection cdata = doc.CreateCDataSection( message );
elem.AppendChild( cdata );
return elem;


# re: Serializing A String Within a CDATA Element (.NET 1.1) 9/14/2006 10:54 PM zheng mu
for your demo code to work in .net 2.0, it needs a little change

void IXmlSerializable.ReadXml(XmlReader reader)

{

this.text = reader.ReadString();
reader.Read(); // a change here
}


# re: Serializing A String Within a CDATA Element (.NET 1.1) 3/7/2007 2:17 AM Balakrishnan S.
Hi
Thank you very much, I found this article V.V. Helpful.

regards,
S. Balakrishnan

# re: Serializing A String Within a CDATA Element (.NET 1.1) 5/22/2007 10:44 AM netty
Really useful post... i ll share more once i ll use in in my problem

# re: Serializing A String Within a CDATA Element (.NET 1.1) 5/13/2008 2:26 AM killuakun
Thank you very much.


# re: Serializing A String Within a CDATA Element (.NET 1.1) 7/18/2008 5:11 PM lenkyl
worked first try. can't thank you enough!!

# re: Serializing A String Within a CDATA Element (.NET 1.1) 11/23/2008 9:56 PM tjb
thx,
非常感谢。

# re: Serializing A String Within a CDATA Element (.NET 1.1) 11/24/2008 1:13 AM tjb
这样写是不是更好玩一些?

/// <summary>
/// 用于序列化CData节点。不能包含]]>字符。
/// </summary>
public class CDATA : IXmlSerializable
{

private string _value;
/// <summary>
///
/// </summary>
public CDATA() { }
/// <summary>
///
/// </summary>
/// <param name="text"></param>
public CDATA(string value)
{
this._value = value;
}
/// <summary>
///
/// </summary>
public string Value
{
get { return _value; }
}

XmlSchema IXmlSerializable.GetSchema()
{
return null;
}

void IXmlSerializable.ReadXml(XmlReader reader)
{
/***** 如果此节点中包含有多个节点须使用次方法。**/
this._value = reader.ReadElementContentAsString();
/* **********/
//this.text = reader.ReadString();
// reader.Read();
}

void IXmlSerializable.WriteXml(XmlWriter writer)
{
writer.WriteCData(this._value);
}
/// <summary>
/// 重写 获取CData节点的 内容
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.Value;
}
/// <summary>
/// 将 CDATA 对象隐式转换成 内容 字符串。
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
public static implicit operator string(CDATA element)
{
return (element == null) ? null : element.Value;
}
/// <summary>
/// 将 内容 对象隐式转换成 CDATA 字符串。
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static implicit operator CDATA(string text)
{
return new CDATA(text);
}

}

# re: Serializing A String Within a CDATA Element (.NET 1.1) 2/19/2009 10:53 AM David
Thank you very much. :)

# re: Serializing A String Within a CDATA Element (.NET 1.1) 10/9/2009 8:54 AM Matt
Cheers for that!

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: