Here is my MC++ Load/Save Xml Code

Below is what 2 functions i made that might help people like me - an MC++ coder

It is working XML load save code based on the ProtoApp

Now all i need is to find a way to save the icons images in the XML (Not as file name references) and ill be able to try it all out :¬)

Hope its of some use to someone ------------------------------------------------------------ -----

int GoDiagram_Load(IO::Stream *file,GoDocument *doc)
{
XmlDocument *xdoc=new XmlDocument();
xdoc->Load(file);
XmlNode *root=xdoc->DocumentElement;
XmlAttribute *a; int docversion=0;
a=dynamic_cast(root->Attributes->GetNamedItem("version"));
if(a!=0)docversion=XmlConvert::ToInt32(a->Value);
if(docversion!=2)return 0; a=dynamic_cast(root->Attributes->GetNamedItem("name"));
if(a!=0)doc->Name=a->Value;
a=dynamic_cast(root->Attributes->GetNamedItem("lastpartid"));
if(a!=0l)doc->LastPartID=XmlConvert::ToInt32(a->Value) ;

System::Collections::IEnumerator *theChildren=root->ChildNodes->GetEnumerator();
while(theChildren->MoveNext()){ XmlNode *item;
try{item=__try_cast(theChildren->Current);}
catch(System::InvalidCastException*){continue;}
if(item->NodeType!=XmlNodeType::Element)continue;

if(String::Compare(item->Name,S"node")==0){
int nkind;
a=dynamic_cast(item->Attributes->GetNamedItem("kind"));
if(a!=0)nkind=XmlConvert::ToInt32(a->Value);
GoSimpleNode *n=new GoSimpleNode();
String *iconname=0;
String *nodename=0;
a=dynamic_cast(item->Attributes->GetNamedItem("nid"));
if(a!=0)n->PartID=XmlConvert::ToInt32(a->Value);
a=dynamic_cast(item->Attributes->GetNamedItem("icon"));
if(a!=0)iconname=a->Value;
a=dynamic_cast(item->Attributes->GetNamedItem("label"));
if(a!=0)nodename=a->Value;
n->Initialize(0,iconname,nodename);
a=dynamic_cast(item->Attributes->GetNamedItem("pidin"));
if(a!=0)n->InPort->PartID = XmlConvert::ToInt32(a->Value);
a=dynamic_cast(item->Attributes->GetNamedItem("pidout"));
if(a!=0)n->OutPort->PartID = XmlConvert::ToInt32(a->Value);
float x=0;
float y=0;
float w=10;
float h=10;
a=dynamic_cast(item->Attributes->GetNamedItem("x"));
if(a!=0)x=XmlConvert::ToSingle(a->Value);
a=dynamic_cast(item->Attributes->GetNamedItem("y"));
if(a!=0)y=XmlConvert::ToSingle(a->Value);
a=dynamic_cast(item->Attributes->GetNamedItem("w"));
if(a!=0)w=XmlConvert::ToSingle(a->Value);
a=dynamic_cast(item->Attributes->GetNamedItem("h"));
if(a!=0)h=XmlConvert::ToSingle(a->Value);
bool sizeIcon=false;
a=dynamic_cast(item->Attributes->GetNamedItem("width"));
if(a!=0){
sizeIcon=true;
w=XmlConvert::ToSingle(a->Value);
}
a=dynamic_cast(item->Attributes->GetNamedItem("height"));
if(a!=0)h=XmlConvert::ToSingle(a->Value);
if(sizeIcon){
n->Icon->Bounds = RectangleF(x,y,w,h);
}else{
n->Bounds=RectangleF(x,y,w,h);
}
doc->Add(n);

}else if(String::Compare(item->Name,"comment")==0){
GoComment *n=new GoComment();
a=dynamic_cast(item->Attributes->GetNamedItem("nid"));
if(a!=0)n->PartID=XmlConvert::ToInt32(a->Value);
a=dynamic_cast(item->Attributes->GetNamedItem("label"));
if(a!=0)n->Text=a->Value;
float x=0;
float y=0;
a=dynamic_cast(item->Attributes->GetNamedItem("x"));
if(a!=0)x=XmlConvert::ToSingle(a->Value);
a=dynamic_cast(item->Attributes->GetNamedItem("y"));
if(a!=0)y=XmlConvert::ToSingle(a->Value);
n->Position=PointF(x,y);
doc->Add(n);

}else if(String::Compare(item->Name,"link")==0){
GoLabeledLink *n=new GoLabeledLink();
a=dynamic_cast(item->Attributes->GetNamedItem("nid"));
if(a!=0)n->PartID=XmlConvert::ToInt32(a->Value);
a=dynamic_cast(item->Attributes->GetNamedItem("from"));
if(a!=0){
int id=XmlConvert::ToInt32(a->Value);
GoPort *p=dynamic_cast(doc->FindPart(id));
if(p!=0)n->FromPort=p;
}
a=dynamic_cast(item->Attributes->GetNamedItem("to"));
if(a!=0){
int id=XmlConvert::ToInt32(a->Value);
GoPort *p=dynamic_cast(doc->FindPart(id));
if(p!=0)n->ToPort=p;
}
doc->LinksLayer->Add(n);
}
} doc->EnsureUniquePartID();
doc->UndoManager=new GoUndoManager();
doc->IsModified=false;
return 1;
} int GoDiagram_Save(IO::Stream *file,GoDocument *doc)
{
XmlTextWriter *writer=new XmlTextWriter(file,0);
writer->Formatting=Formatting::Indented;
writer->WriteStartElement("graph");
writer->WriteAttributeString("version","2");
writer->WriteAttributeString("name",doc->Name);
writer->WriteAttributeString("lastpartid",XmlConvert::ToS tring(doc->LastPartID));

GoLayerCollectionObjectEnumerator thePain=doc->GetEnumerator();
GoLayerCollectionObjectEnumerator *theChildren=&thePain; theChildren->Reset();
while(theChildren->MoveNext()){
GoSimpleNode *n;
try{n=__try_cast(theChildren->Current);}
catch(System::InvalidCastException*){continue;}
writer->WriteStartElement("node");
writer->WriteAttributeString("nid",XmlConvert::ToString(n ->PartID));
if(n->InPort!=0)writer->WriteAttributeString("pidin",X mlConvert::ToString(n->InPort->PartID));
if(n->OutPort!=0)writer->WriteAttributeString("pidout" ,XmlConvert::ToString(n->OutPort->PartID));
writer->WriteAttributeString("icon",n->Image->Name) ;
writer->WriteAttributeString("label",n->Text);
writer->WriteAttributeString("x",XmlConvert::ToString(n-& amp; gt;Icon->Left));
writer->WriteAttributeString("y",XmlConvert::ToString(n-& amp; gt;Icon->Top));
writer->WriteAttributeString("width",XmlConvert::ToString (n->Icon->Width));
writer->WriteAttributeString("height",XmlConvert::ToStrin g(n->Icon->Height));
writer->WriteEndElement();
}

theChildren->Reset();
while(theChildren->MoveNext()){
GoComment *n;
try{n=__try_cast(theChildren->Current);}
catch(System::InvalidCastException*){continue;}
writer->WriteStartElement("comment");
writer->WriteAttributeString("nid",XmlConvert::ToString(n ->PartID));
writer->WriteAttributeString("label",n->Text);
writer->WriteAttributeString("x",XmlConvert::ToString(n-& amp; gt;Left));
writer->WriteAttributeString("y",XmlConvert::ToString(n-& amp; gt;Top));
writer->WriteEndElement();
} theChildren->Reset();
while(theChildren->MoveNext()){
GoLabeledLink *n;
try{n=__try_cast(theChildren->Current);}
catch(System::InvalidCastException*){continue;}
writer->WriteStartElement("link");
writer->WriteAttributeString("nid",XmlConvert::ToString(n ->PartID));
if(n->FromPort!=0){
GoPort *p=dynamic_cast(n->FromPort->GoObject);
writer->WriteAttributeString("from",XmlConvert::ToString( p->PartID));
}
if(n->ToPort!=0){
GoPort *p=dynamic_cast(n->ToPort->GoObject);
writer->WriteAttributeString("to",XmlConvert::ToString(p- >PartID));
}
writer->WriteEndElement();
} writer->WriteEndElement();
writer->Close();
return 1;
}

http://www.nwoods.com/forum/forum_posts.asp?TID=456