-3

I have to create an serializable class which has and xml in the format mentioned below. Please check the xml on this link and the ExtendedAttribute Element.

http://telligent.com/community/developers/w/developer6/update-a-user.aspx

The following tag is formed as and key value pair and does not have an object

My ExtendedAtribute class is not a fixed class but its an key value type object which may increase and decrease dynamically

halfer
  • 19,824
  • 17
  • 99
  • 186
Selwyn
  • 1,621
  • 6
  • 21
  • 38
  • 1
    You need to tidy that up, it's unreadable. –  May 14 '12 at 09:54
  • Put your xml into a file, say, `my.xml`, then - at the command line: xsd my.xml xsd my.xsd /classes And then `my.cs` has the code. Or just write the class manually: public class ExtendedAttributes { public bool EnableDisplayName {get;set;} ... public int CPPageSize {get;set;} ... In either case, use `new XmlSerializer(typeof(ExtendedAttributes)).Serialize(...)` etc. – Marc Gravell May 14 '12 at 09:55
  • my ExtendedAtribute class is not a fixed class but its an key value type object which may increase and decrease dynamically – Selwyn May 14 '12 at 10:01
  • @Selwyn then why are you asking about classes? Just use a DOM – Marc Gravell May 14 '12 at 10:05
  • i had used seriaziable classes for each of the elements only for the ExtendedAtrribute i havent used can you please help me how can i extract these dynamic element attributes – Selwyn May 14 '12 at 10:08
  • @Selwyn I don't understand what you are asking – Marc Gravell May 14 '12 at 10:10
  • Ah, I see, you have a list of attributes that may be bool, equivalent to Dictionary and you want to know how to serialise and dessrialise that such that the keys are the elements and the values the content of the elements? –  May 14 '12 at 10:13
  • I have seriaziable classes for each of the elements and i use xml seriaziable to deserialize the object but my major issue is for the extended properties since they are in the key value pair form tags that are dynamic and i could not deserialized to a fixed seriaziable class did i clear your doubt – Selwyn May 14 '12 at 10:13
  • @JonB yes you got it right .. – Selwyn May 14 '12 at 10:14
  • Do you need both directions or just to deserialise? –  May 14 '12 at 10:15
  • both but the deserialize is the the one i need the most since its a road block – Selwyn May 14 '12 at 10:16
  • I think it's a dupe of http://stackoverflow.com/questions/495647/serialize-class-containing-dictionary-member See the top answer there. –  May 14 '12 at 10:19

1 Answers1

1

For the reduced XML File:-

<ExtendedAttributes>
  <EnableDisplayName>True</EnableDisplayName>
  <EditorType>Enhanced</EditorType>
  <EnableConversationNotifications>True</EnableConversationNotifications>
  <EnableUserSignatures>True</EnableUserSignatures>
  <CPPageSize>10</CPPageSize>
  <EnableActivityMessageNewUserAvatar>True</EnableActivityMessageNewUserAvatar>
  <EnableActivityMessageThirdPartyMessageType>True</EnableActivityMessageThirdPartyMessageType>
  <EnableStartConversations>1</EnableStartConversations>
  <avatarUrl>~/cfs-file.ashx/__key/communityserver-components-selectableavatars/03b2c875-fbfb-4d26-8000-ef001b9f4728/avatar.png</avatarUrl>
  <EnableActivityMessageNewProfileComment>False</EnableActivityMessageNewProfileComment>
  <EnableActivityMessageStatus>True</EnableActivityMessageStatus>
</ExtendedAttributes>

You can parse it with:-

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

  [XmlRoot("ExtendedAttributes")]
  public class SerialisableDictionary : Dictionary<string, string>, IXmlSerializable
  {
    #region IXmlSerializable Members
    public System.Xml.Schema.XmlSchema GetSchema()
    {
      return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
      reader.Read();
      while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
      {
        string key = reader.Name;
        this.Add(key, reader.ReadElementContentAsString());
        reader.MoveToElement();
      }
      reader.ReadEndElement();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
      // TODO
    }
    #endregion
  }

  class Program
  {
    static void Main(string[] args)
    {
      SerialisableDictionary sd = new SerialisableDictionary();
      XmlSerializer x = new XmlSerializer(sd.GetType());
      using (StreamReader sr = new StreamReader(@"XMLFile1.xml"))
      {
        sd = (SerialisableDictionary)x.Deserialize(sr);
      }
      foreach(var kvp in sd)
      {
        Console.WriteLine(kvp.Key + " = " + kvp.Value);
      }
      Console.WriteLine("Done.");
      Console.ReadKey();
    }
  }

This gives you a Dictionary<string, string> you almost certainly want the true/false/string/number values parsed, but that's another problem.

I appreciate this isn't perfect, but it should be enough to get you going. Unfortunately it will get quite involved and I don't have much time.

All based heavily on the answer in Serialize Class containing Dictionary member

Community
  • 1
  • 1