Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Problem with LoadFromXml (Read 2090 times)
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Problem with LoadFromXml
Aug 9th, 2010 at 2:20pm
Print Post  
Hi everybody.
I've got a problem with LoadfromXml().
[System.NullReferenceException]
I've read in the manual that we must create a class for custom items so i did it :
class IconNode : DiagramNode
{
...
}
But  nothing could solve the problem.
Any help would be welcomed.

  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Problem with LoadFromXml
Reply #1 - Aug 9th, 2010 at 2:24pm
Print Post  
Hi,

Have you added a call to RegisterItemClass? You can see how the IconNodes sample does that in the MainForm constructor. Additionally, the class should have a no-arg constructor or one that takes a Diagram argument.

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


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Re: Problem with LoadFromXml
Reply #2 - Aug 10th, 2010 at 7:00am
Print Post  
Yes i've added a call to RegisterItemClass() in the winform constructor.
hmm ... i added a no-arg constructor but no changes...

Do i have to create one item class for each custom node in my xml document or the iconNode class work for all customs nodes .

I'll give you my method; could you tell me if you see a problem please ?

My xml document :
<?xml version="1.0" encoding="utf-8" ?>
<body>
<ass8f>
<ass4f-1>
</ass4f-1>
..............
<ass4f-2>
</ass4f-2>
<ass8f>
<body>

my form constructor :
Diagram.RegisterItemClass(typeof(EspaceDeNomsBDB.Classes_Outils.IconNode), "IconNode", 1);

my icon node class :
class IconNode : DiagramNode
{

static Image defaultIcon = Bitmap.FromFile("C:\\BDB\\images\\conforme.bmp");

Image icon ;
string label ;
StringFormat format ;

static IconNode()
{
defaultIcon = new Bitmap(48, 48);

Graphics graphics = Graphics.FromImage(defaultIcon);
Font font = new Font("Arial", 48, FontStyle.Bold, GraphicsUnit.Pixel);

graphics.FillRectangle(Brushes.Transparent, 0, 0, 48, 48);
graphics.DrawString("?", font, Brushes.Black, 0, 0);

font.Dispose();
graphics.Dispose();
}

public IconNode()
{

}

public IconNode(Diagram diagram): base(diagram)
{
icon = defaultIcon;
label = "Composant";

format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;

Bounds = new RectangleF(Bounds.Location,CalculateSize());
}

public override void Draw(IGraphics graphics, RenderOptions options)
{
Rectangle iconSizePixels = new Rectangle(
0, 0, icon.Width, icon.Height);
RectangleF imageSize = MindFusion.Utilities.DeviceToDoc(
graphics, iconSizePixels);

// Draw the icon at the top-middle
graphics.DrawImage(icon,
Bounds.X + Bounds.Width / 2 - imageSize.Width / 2, Bounds.Y);

// Draw the label at the bottom
RectangleF labelBounds = RectangleF.FromLTRB(
Bounds.X, Bounds.Y + imageSize.Height,
Bounds.Right, Bounds.Bottom);

graphics.DrawString(label,
Font, Brushes.Black, labelBounds, format);
}
public override void DrawShadow(IGraphics graphics, RenderOptions options)
{

}
public Image Icon
{
get { return icon; }
set
{
icon = value;
Bounds = new RectangleF(Bounds.Location, CalculateSize());
}
}

private SizeF CalculateSize()
{
SizeF aaa = new SizeF(defaultIcon.Width, defaultIcon.Height);
return aaa;
}

public string Label
{
get { return label; }
set
{
label = value;
Bounds = new RectangleF(Bounds.Location, CalculateSize());
}
}



protected override void SaveTo(System.IO.BinaryWriter writer, PersistContext ctx)
{
base.SaveTo(writer, ctx);

// Save the label using the standard .NET BinaryWriter
writer.Write(label);

// Save the image using the MindFusion.Diagramming built-in image saving code,
// which stores the contents of shared images only once.
ctx.SaveImage(icon);
}

protected override void LoadFrom(System.IO.BinaryReader reader, PersistContext ctx)
{
base.LoadFrom(reader, ctx);
label = reader.ReadString();
icon = ctx.LoadImage();
}

my method that load the xml file :
private void button1_Click(object sender, EventArgs e)
{
string path = "C:\\BDB\\exploitation\\view.xml";
diag_view.LoadFromXml(path);
}

Is there any problem here ?

Please note that all the titles of nodes are serial numbers except the body. Do you think it can work ? because if i understood correctly i have to do a call to RegisterItemClass() in the form constructor but at that moment the program doesn't know the serial numbers that will be called so it doesn't know the title.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Problem with LoadFromXml
Reply #3 - Aug 10th, 2010 at 7:28am
Print Post  
LoadFromXml can only load files created by the diagram's SaveToXml method. If you need to create nodes from arbitrary xml data, check these samples:

http://www.mindfusion.eu/onlinehelp/wpfdiagram/index.htm?CC_Tutorial_1__Loading_...
http://www.mindfusion.eu/onlinehelp/wpfdiagram/index.htm?CC_Tutorial_2__Loading_...
  
Back to top
 
IP Logged
 
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Re: Problem with LoadFromXml
Reply #4 - Aug 10th, 2010 at 1:09pm
Print Post  
Tanks for all, that work Smiley
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint