This topic is for advance users, and many areas will not be explained. The Functioning code traverses a SC2Lib file created with SC2 map editor.
An SC2Lib file is essentialy a XML file. Using Visual studios System.Xml Imports lets us traverse the Structure of a SC2 Library.
This tutorial will be limited in only ready the file into Flat XML. The reconstruction of the library into a SC2 Map Editor view may be saved for Tutorial 3.
And now for the Show.
An SC2Lib file contains 6 major areas.
XML Opening Encoding, TriggerDATA, Root, Items, Elements, Element Value
Each of these areas are essentialy nested. The structure looks like what follows.
-XML Opening Encoding
TriggerDATA
Root
Item1
Item2
Item3
Element1
Element Value1
Element Value2
Element Value3
Element2
Element Value1
Element Value2
Element Value3
Element3
Element Value1
Element Value2
Element Value3
The root is everything contained within the main Library Folder inside the SC2 Map Editor. Following the root are nested elements.
To Traverse from opening the XML Document, we need to search Xelements, and Xelements Xelements. These Elements may contain Attributes, and if an Element does not contain Elements, then we search for Xnodes and its respective value must be used.
The Following is a logical traverse of a SC2Lib file.
By following this path, we visit all areas of a SC2Lib file. Now here is the results of looking at Built in Library.
The code for this project is commented below. There are a number of routines to help this be a fuctioning program.
-Form Loading
Prepare Image list for TreeView
Set set a status picture on the form to blank
-Resizing the form
Scale the TreeView based on the form size
-Load image list
Search for image files located in Icons folder.
Prepare treeview1
-Set TreeNode
Customize Settings
Return New TreeNode
-Add TreeNode to TreeView
Create Delegate to handle Cross Thread comunication with the Form Thread
Add TreeNode to TreeView
Do other Thread Operations
-Browse Button Clicked
Open XML File
-Traverse XML File
Create a New Thread to Compute XML File
Start Thread
Open XML File
Nested Loop Elements
Loop Nodes
Attributes
Elements
The following code is commented directly from the Project File.
ImportsSystem.IOImportsSystem.XmlImportsSystem.ThreadingImportsSystem.DrawingPublicClassfrmSC2Lib_XMLDimxmlFAsFileInfo'xml file infoDimxmlDocAsNewXDocument'xml DocumentDimImgListAsImageList'treeview image listDimt1AsThread'Traverse ThreadPrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.LoadLoadImageList()'run sub Load treeview image collectionMe.PictureBox1.Image=ImgList.Images("Blank")'Form Status ImageEndSubSubLoadImageList()'Load TreeView Icons (Every Gif, and BMP in folder Icons is loaded)ImgList=NewImageListImgList.ImageSize=NewSize(24,24)DimdAsDirectoryInfo=NewDirectoryInfo(Application.StartupPath&"\Icons")ForEachfInd.GetFilesImgList.Images.Add(f.Name.Replace(".bmp","").Replace(".gif",""),Image.FromFile(f.FullName))NextMe.TreeView1.ImageList=ImgListEndSubPrivateSubForm1_Resize(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesMe.ResizeMe.TreeView1.Size=NewSize(Me.Width-(347-307),Me.Height-(474-383))'TreeView ResizeEndSubPrivateSubbtnBrowse_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesbtnBrowse.ClickDimdrAsDialogResult=Me.OpenFileDialog1.ShowDialog'Send Open File DialogIfdr=Windows.Forms.DialogResult.OKThen'If file was selectedMe.TreeView1.Nodes.Clear()'Clear the TreeViewxmlF=NewFileInfo(OpenFileDialog1.FileName)'Load the XML file infolblFileName.Text=xmlF.Name'Change the form Label to match the file being openedTraverseXML()'Start Travering the XML FileEndIfEndSubDimXMLImgAsImageSubTraverseXML()'Starts a Thread for Traversing the XML fileXMLImg=Image.FromFile(Application.StartupPath&"\Icons\Build.gif")'get Animated GifMe.PictureBox1.Image=XMLImg'Form Status Imaget1=NewThread(AddressOfOpenXml)'Create a Thread for opening xmlt1.Start()'Start OpenXml threadEndSubSubOpenXml()'Thread SubDimXmlReaderAsXmlReader=XmlReader.Create(xmlF.FullName)'Create an XML ReaderxmlDoc=XDocument.Load(XmlReader)'Read the whole XML documentDimiNodeAsTreeNode=NodeSet(xmlF.Name,"XML")'Create TreeNode for DocumentForEachxEInxmlDoc.Elementsxmlnode(xE,iNode)'Nested Loop Elements sending both element and parent TreeNodeNextAddNodes(iNode)'Send Parent Node to TreeViewEndSubPublicDelegateSubAddNode(ByRefobjAsObject)'Delegate Instance on main ThreadSubAddNodes(ByRefobjAsObject)'Cross Thread activator, Add Main TreeNode to TreeViewIfMe.TreeView1.InvokeRequiredThen'Check for Cross Thread violationDimadAsAddNode=NewAddNode(AddressOfAddNodes)'SetUp DelegateMe.TreeView1.Invoke(ad,obj)'Invoke Thread on TreeView1 ThreadElse'Invoking on main ThreadMe.PictureBox1.Image=ImgList.Images("Construct")'Form Status ImageMe.TreeView1.BeginUpdate()'Ready the TreeViewMe.TreeView1.Nodes.Add(obj)'Add Parent Node to TreeViewMe.TreeView1.EndUpdate()'End Ready the TreeViewMe.PictureBox1.Image=ImgList.Images("Built")'Form Status ImageEndIfEndSubFunctionNodeSet(ByRefNameAsString,ByRefTypeAsString)AsTreeNode'Prepare TreeNodeDimTreeNodeAsTreeNode=NewTreeNode(Name)'Create a New TreeNode displaying text NameTreeNode.ImageKey=Type'TreeNode Picture from TypeTreeNode.SelectedImageKey=Type'TreeNode Picture from TypeReturnTreeNode'Return a prepared TreeNodeEndFunctionSubxmlnode(ByRefxEAsXElement,ByRefTreeNodeAsTreeNode)DimiNodeAsTreeNode=NodeSet(xE.Name.ToString,"Element")'Create TreeNode ElementTreeNode.Nodes.Add(iNode)' Add TreeNode Element to Parent TreeNodeIfNotxE.HasElementsThen'NLevel ElementForEachxNInxE.Nodes'Loop Nodes for ValuesiNode.Nodes.Add(NodeSet(xN.ToString,"Value"))'Add TreeNode Value to TreeNode ElementNextEndIfForEachxAttInxE.Attributes'Loop Attributes for ValuesiNode.Nodes.Add(NodeSet(xAtt.ToString,"Attribute"))'Add TreeNode Attribute to TreeNode ElementNextForEachxEnestInxE.Elements'Loop Elements for all of the abovexmlnode(xEnest,iNode)'Nested ElementsNextEndSubEndClass
And that’s how simple it is. Where it would get complicated is traversing from the Root location. I may save this for Tutorial 3 depending on community support.
Well, this part is not very useful without a Part III :)
Also, I think it's a bit low on explanation. I happen to have made some XML parsing lately so I understand well what the code does, but I think others will have a hard time following it. This is also only the very basic of a xml reader (for example, how to get this stuff in a nice tree-like view like you got there).
Lastly, it'd be great if you could use the code-tags for scripts.
I edited your post and I think you can see the improvements.
Best code-tags for this language are the lua tags, I'd say:
ReadIng XML Library [ SC2Lib ] Visual Studio Tutorial 2
This tutorial assums you know the basics from Tutorial 1.
http://forums.sc2mapster.com/resources/tutorials/14517-tutorial-creating-a-visual-studio-program-to-write/
This topic is for advance users, and many areas will not be explained. The Functioning code traverses a SC2Lib file created with SC2 map editor.
An SC2Lib file is essentialy a XML file. Using Visual studios System.Xml Imports lets us traverse the Structure of a SC2 Library.
This tutorial will be limited in only ready the file into Flat XML. The reconstruction of the library into a SC2 Map Editor view may be saved for Tutorial 3.
And now for the Show.
An SC2Lib file contains 6 major areas.
XML Opening Encoding, TriggerDATA, Root, Items, Elements, Element Value
Each of these areas are essentialy nested. The structure looks like what follows.
-XML Opening Encoding
TriggerDATA
Root
Item1
Item2
Item3
Element1
Element Value1
Element Value2
Element Value3
Element2
Element Value1
Element Value2
Element Value3
Element3
Element Value1
Element Value2
Element Value3
The root is everything contained within the main Library Folder inside the SC2 Map Editor. Following the root are nested elements.
To Traverse from opening the XML Document, we need to search Xelements, and Xelements Xelements. These Elements may contain Attributes, and if an Element does not contain Elements, then we search for Xnodes and its respective value must be used.
The Following is a logical traverse of a SC2Lib file.
By following this path, we visit all areas of a SC2Lib file. Now here is the results of looking at Built in Library.
The code for this project is commented below. There are a number of routines to help this be a fuctioning program.
-Form Loading
Prepare Image list for TreeView
Set set a status picture on the form to blank
-Resizing the form
Scale the TreeView based on the form size
-Load image list
Search for image files located in Icons folder.
Prepare treeview1
-Set TreeNode
Customize Settings
Return New TreeNode
-Add TreeNode to TreeView
Create Delegate to handle Cross Thread comunication with the Form Thread
Add TreeNode to TreeView
Do other Thread Operations
-Browse Button Clicked
Open XML File
-Traverse XML File
Create a New Thread to Compute XML File
Start Thread
Open XML File
Nested Loop Elements
Loop Nodes
Attributes
Elements
The following code is commented directly from the Project File.
And that’s how simple it is. Where it would get complicated is traversing from the Root location. I may save this for Tutorial 3 depending on community support.
Here is the asset when it is approved
http://www.sc2mapster.com/assets/reading-sc2lib-xml-tutorial-visu/
Happy Coding Sldprt (ItsAboutTime)
This is awesome... Definitely going to be useful, great job.
Well, this part is not very useful without a Part III :)
Also, I think it's a bit low on explanation. I happen to have made some XML parsing lately so I understand well what the code does, but I think others will have a hard time following it. This is also only the very basic of a xml reader (for example, how to get this stuff in a nice tree-like view like you got there).
Lastly, it'd be great if you could use the code-tags for scripts. I edited your post and I think you can see the improvements.
Best code-tags for this language are the lua tags, I'd say: