by Keyvan Nayyeri via Keyvan Nayyeri on 8/11/2006 8:32:57 AM
As I said before one of new features in new version of BlogML is Blog Extended Properties. Extended Properties are a collection of property names and their corresponding values about blogs.
Recently I tried to implement them in .NET APIs. At first glance I started with IDictionary<string, string> and implemented this interface and inherited my Extended Properties class from this base class but after seeing this post from Darren, removed that implementation and tried NameValueCollection. the problem with NameValueCollection is it's not Serializable by default.
In this KB article Microsoft has described this situation and has recommended a solution to use SoapFormatter to Serialize and Deserialize NameValueCollections. But as we serialize everything manually in BlogML I couldn't use it. In the process of finding a solution, I found this article by James Crowley about a technique to serialize NameValueCollections but it didn't fit to my needs so I started modifying it. While I was working on this and couldn't get any result I talked to Darren about this problem and he decided to implement Extended Properties as List<SerializableKeyValuePair<string, string>> and did it.
Today I worked more on my approach to make it work and could get the result.
First I created a Serializable class for my extended_properties then implemented IXmlSerializable interface for it. This class contains a NameValueCollection as a property but I marked it as XmlIgnore to ignore it from Serialization. Instead, I serialized it manually using the interface and the result was following class:
using System;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace NameValueCollectionSerializing
{
[Serializable]
public class extended_properties : IXmlSerializable
#region Private members
private NameValueCollection properties;
#endregion
#region Constructors
public extended_properties()
}
public extended_properties
(NameValueCollection properties)
this.properties = properties;
#region Properties
[XmlIgnore]
public NameValueCollection Properties
get { return this.properties; }
set { this.properties = value; }
#region IXmlSerializable Members
XmlSchema IXmlSerializable.GetSchema()
return null;
void IXmlSerializable.ReadXml(XmlReader reader)
this.properties = new NameValueCollection();
while (reader.MoveToNextAttribute())
this.properties.Add
(reader.Name, reader.Value);
reader.Read();
void IXmlSerializable.WriteXml(XmlWriter writer)
foreach (string key in this.properties.Keys)
writer.WriteStartElement("property");
string value = this.properties[key];
writer.WriteAttributeString(key, value);
writer.WriteEndElement();
After implementing this class, I could get what I wanted. The code to test this class is:
private void button1_Click(object sender, EventArgs e)
string FilePath = Path.GetDirectoryName
(Application.ExecutablePath)
+ "\\ExtendedProperties.xml";
NameValueCollection Properties =
new NameValueCollection();
Properties.Add("Trackbackenabled", "True");
Properties.Add("CommentModeartion", "1 Month");
extended_properties exProperties =
new extended_properties(Properties);
XmlSerializer xs = new XmlSerializer
(typeof(extended_properties));
StreamWriter sw = new StreamWriter(FilePath);
xs.Serialize(sw, exProperties);
sw.Close();
extended_properties exProperties2;
StreamReader sr = new StreamReader(FilePath);
exProperties2 =
(extended_properties)xs.Deserialize(sr);
sr.Close();
System.Diagnostics.Process.Start
("notepad", FilePath);
Now playing: Michael Jackson - Billie Jean
Original Post: Serialize NameValueCollection
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.