different minds about different platforms

a blog of Dirk Eisenberg (>)

  Home  |   Contact  |   Syndication    |   Login
  47 Posts | 4 Stories | 7 Comments | 17 Trackbacks

News

Ich hoffe das jeder Besucher in diesem Blog das ein oder andere interessante Thema findet. Als Autor der Beiträge freue ich mich besonders über Feedback in Form von Kommentaren oder auch persönlicher als E-Mail. Dankbar nehme ich Anregungen und Korrekturen von fehlerhaften Inhalten entgegen.

Article Categories

Archives

Post Categories

Image Galleries

Blogs

Links

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);
  }
 }
}

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