Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic XML (Read 2234 times)
DSspirit
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 10
Joined: Nov 3rd, 2009
XML
Nov 9th, 2009 at 6:39pm
Print Post  
Hey,

So I'm just starting to familiarize myself with xml and the SaveToXml and LoadFromXml methods, and thus far they seem to work just fine for serializing the diagramitems, however not their tags. If you tag a diagramnode or any item to a seperate user defined class object, how do you serialize both classes and their connections to each other? Hope someone can help.

Thanks,
Dave
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: XML
Reply #1 - Nov 9th, 2009 at 7:42pm
Print Post  
Hi,

The control provides built-in serialization only for simple value types. It raises the SerializeTag and DeserializeTag events to let you save tags of other types. Use the XML DOM API to save the tag objects as elements and attributes under the tag's element you receive as event argument.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
DSspirit
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 10
Joined: Nov 3rd, 2009
Re: XML
Reply #2 - Nov 10th, 2009 at 3:28pm
Print Post  
I was just wondering if this is the correct format for serializing a tagged object. The way I have written it I would have to write out each property of taggedElement using e.Context.WriteString, which can be tedious.


Code
Select All
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Write("file.xml")
    End Sub

    Private Sub Write(ByVal fileName As String)

        AddHandler diagram.SerializeTag, New SerializeTagEventHandler(AddressOf SerializeTag)
        diagram.SaveToXml(fileName)

    End Sub

    Sub SerializeTag(ByVal sender As Object, ByVal e As SerializeTagEventArgs)

        e.Handled = True

        Dim taggedElement As Elem = e.Tag
        Dim stringRepresentation As String = ""

        If Not taggedElement Is Nothing Then

            stringRepresentation = "ID"
            'stringRepresentation += ";"
            stringRepresentation += taggedElement.ID.ToString()

        End If

        Dim repElem As XmlElement = e.Context.XmlDocument.CreateElement(stringRepresentation)
        e.Representation.AppendChild(repElem)
        'e.Context.WriteObject(taggedElement, stringRepresentation, repElem)
        e.Context.WriteString(taggedElement.RandomNumber.ToString(), "RandomNumber", repElem)


    End Sub 



Thanks,
Dave
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: XML
Reply #3 - Nov 10th, 2009 at 4:52pm
Print Post  
If you don't want to call e.Context.Write for each property, you might use reflection to write all properties in a single loop. E.g. this code is from the ControlNode.SaveToXml implementation:

Code
Select All
Type type = control.GetType();
string assemblyName = type.Assembly.GetName().Name;
string typeName = type.FullName;

controlElement.SetAttribute("AssemblyName", assemblyName);
controlElement.SetAttribute("TypeName", typeName);

XmlElement propertiesElement =
	context.AddChildElement("Properties", controlElement);

// Save control serializable properties
PropertyInfo[] props = type.GetProperties();
foreach (PropertyInfo prop in props)
{
	if (!prop.CanRead || !prop.CanWrite)
		continue;
	if (FilteredProperty(prop))
		continue;

	object val;
	try
	{
		val = prop.GetValue(control, null);
	}
	catch
	{
		// Some properties cannot be read
		continue;
	}

	if (val == null)
		continue;

	if (!(val.GetType().IsSerializable))
		continue;

	context.WriteObject(val, prop.Name, propertiesElement);
}
 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
DSspirit
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 10
Joined: Nov 3rd, 2009
Re: XML
Reply #4 - Nov 10th, 2009 at 8:22pm
Print Post  
Thanks, that's perfect!
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint