Step-by-Step into the cloud

a blog of Dirk Eisenberg (>)
posts - 81, comments - 15, trackbacks - 17

My Links

News

Article Categories

Archives

Post Categories

Image Galleries

Blogs

Projects

A generic algorithm to convert WinFS-items to XML

This sample demonstrates a solution to serialize a WinFS-item into a XML-representation. It's only a proposal and many features are missing.

using System;
using System.Storage;
using System.Storage.Contacts;
using System.Storage.Core;
using System.Collections.Generic;
using System.Text;
using System.Storage.Documents;

using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;


namespace WinFSConsoleApplication2
{
 /*
  * This is our serializable object to convert
  * every WinFS-item-object to there xml representation.
  */
 public class WinFSXMLItem : IXmlSerializable
 {
  /*
   * ISerializable implementation
   */
  public XmlSchema GetSchema()
  {
   return null;
  }

  public void ReadXml(XmlReader reader)
  {
  }

  public void WriteXml(XmlWriter writer)
  {
   Type t = m_item.GetType();

   writer.WriteStartElement("WinFSItem");
   writer.WriteAttributeString("type",t.Name.ToString() );
   writer.WriteAttributeString("module", t.Module.ToString());

   System.Reflection.PropertyInfo[] pinfos = t.GetProperties();
   for (int i = 0; i < pinfos.Length; i++)
   {
    System.Reflection.PropertyInfo pinf = pinfos[i];

    String strValue = "";
    String strType = "";
    String strName = "";

    try
    {
     strValue = pinf.GetValue(m_item, null).ToString();
     strType = pinf.PropertyType.ToString();
     strName = pinf.Name;
    } catch( Exception e)
    {
     continue;
    }

    if (strName.Length == 0 || strType.Length == 0 || strValue.Length == 0)
     continue;

    writer.WriteStartElement("ItemProperty");
    writer.WriteAttributeString("Name", strName);
    writer.WriteAttributeString("Type", strType);
    writer.WriteCData(strValue);
    writer.WriteEndElement();
   }

   writer.WriteEndElement();
  }

  public System.Storage.Item item
  {
   set{ m_item = value;}
   get{ return m_item;}
  }

  protected System.Storage.Item m_item;
 }

 /*
  * The WinFSObject-Serialize as self
  */
 public class WinFSXMLSerializer : XmlSerializer
 {
  public WinFSXMLSerializer()
  : base( typeof( WinFSXMLItem ) )
  {}

  public void Serialize(XmlTextWriter writer, System.Storage.Item ob)
  {
   WinFSXMLItem xmlitem = new WinFSXMLItem();
   xmlitem.item = ob;
   base.Serialize(writer, xmlitem);
  }
 }

 /*
  *
  */
 class Program
 {
  static void Main(string[] args)
  {
   Person p = new Person();
   p.BirthDate = new DateTime(1979, 07, 01);

   Document doc = new Document();
   doc.Title = "Hello World";

   WinFSXMLSerializer xmlser = new WinFSXMLSerializer();
   XmlTextWriter writer = new XmlTextWriter( "C:\\test.xml", null );
   //xmlser.Serialize(writer, p );
   xmlser.Serialize(writer, doc);
  }
 }
}

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Friday, September 02, 2005 1:31 PM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: