Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > XML Help
 

XML Help

I have the following code that generates a XML file.

ImportsSystem.Xml
ModuleModule3
PublictrcxmlAsNewXmlDocument
PublictrcdeclareAsXmlDeclaration
PublictrcrootAsXmlElement=trcxml.CreateElement("TrCompanion")
PublictrcserverAsXmlElement
PublictrccharAsXmlElement
PublictrclogosAsXmlElement
PublictrclogoskeyAsXmlElement
PublicSubCreatexml(ByValserverAsString,ByValcharacterAsString)
'trcxml=NewXmlDocument
trcdeclare=trcxml.CreateXmlDeclaration("1.0","UTF-8",String.Empty)
trcxml.InsertBefore(trcdeclare,trcxml.DocumentElement)
'trcroot=trcxml.CreateElement("TrCompanion")
trcxml.InsertAfter(trcroot,trcdeclare)
trcserver=trcxml.CreateElement("Server")
trcserver.SetAttribute("ID",server)
trcroot.AppendChild(trcserver)
trcchar=trcxml.CreateElement("Character")
trcchar.SetAttribute("Name",character)
trcserver.AppendChild(trcchar)
trclogos=trcxml.CreateElement("Logos")
'ForEachkvpAsKeyValuePair(OfString,TRLogos._logos)Inmylogos.LC
'trclogoskey=trcxml.CreateElement("LogosKey")
'trclogoskey.SetAttribute("Name",kvp.Key)
'trclogoskey.SetAttribute("Acq",kvp.Value.Acquired)
'trclogos.AppendChild(trclogoskey)
'Next
trcchar.AppendChild(trclogos)
trcserver=trcxml.CreateElement("Server")
trcserver.SetAttribute("ID","Orion")
trcroot.AppendChild(trcserver)
trcchar=trcxml.CreateElement("Character")
trcchar.SetAttribute("Name","test")
trcserver.AppendChild(trcchar)
trclogos=trcxml.CreateElement("Logos")
trcchar.AppendChild(trclogos)
trcserver=trcxml.CreateElement("Server")
trcserver.SetAttribute("ID","Pegasus")
trcroot.AppendChild(trcserver)
trcchar=trcxml.CreateElement("Character")
trcchar.SetAttribute("Name","test1")
trcserver.AppendChild(trcchar)
trclogos=trcxml.CreateElement("Logos")
trcchar.AppendChild(trclogos)
trcxml.Save(My.Application.Info.DirectoryPath&"\TrCompanion.xml")
EndSub
EndModule
The code is called via a button with Call Createxml("Pegasus","Test")

This is what the file looks like when generated.

<?xmlversion="1.0"encoding="UTF-8"?>
<TrCompanion>
<ServerID="Pegasus">
<CharacterName="Test">
<Logos/>
</Character>
</Server>
<ServerID="Orion">
<CharacterName="test">
<Logos/>
</Character>
</Server>
<ServerID="Pegasus">
<CharacterName="test1">
<Logos/>
</Character>
</Server>
</TrCompanion>


Now for a couple of questions.

  1. Why is last node being added to the bottom and not mixed with the First node with the same "ID"?
  2. Is this the best way to create the file? The Server tag will have 4 Unique "ID"'s max, The Character tag can have as many as 16 Unique "Names" per Server with the same name possible on different Servers.
  3. How would you add a Schema to the file and project to be used for validation of the file? The Schema was generated with the XML-Schema Tool and is in the root directory of the project.
Any other Suggestions or thoughts?

Please forgive any mistakes or thinking as still new to VB and XML in general.

Thanks,
Steve

SteveHart  Friday, July 11, 2008 1:13 PM
Just to bump this, Nobody has any answers to my questions?

I have it worked out on the Server node by finding if the node exists and then setting that as the Server element.

I could really use some help with the other questions though.

Thanks,
Steve
SteveHart  Monday, July 14, 2008 10:58 PM
Hi Steve,

What you really want to do appears to be what you commented out, the loop. What version of VB are you using? If you got 2008 and 3.5 framework, you should really look into LINQ.

I have an example on my web site of parsing XML utilizing LINQ. I haven't gotten to writing to it yet, but if you look at the vb how-to by Beth Massi,http://msdn.microsoft.com/en-us/vbasic/bb466226.aspxgo to the LINQ part with xml. She utilizes something called an XDocument, and the parsing with LINQ is an IENumerable. She also parses into a class, something I do also, and then bind the class.

That's enough to get you going, if you are working with an earlier version of VB, you may want to look into the nodeslist, so you can get your looping going.

I hope you find that helpful,

John.
Visit my web site at: http://RadiolistenersElectricEasel.com/ If a post is helpful please give it a vote of helpful
RadioListener  Monday, July 14, 2008 11:08 PM
The For.Each loop was commented out for simplicity in the document for testing as that generates 390 elements.

I am using VS08. The app was written to target .Net 2.0 so that a wider range of people could use it without having to upgrade to .Net 3.5.

Steve
SteveHart  Monday, July 14, 2008 11:46 PM
Hi Steve,

Sorry, really not sure. I would do it as an Xdocument. Your way I haven't worked with that much.

I hope you find the answer.

Best of Luck,

John.
Visit my web site at: http://RadiolistenersElectricEasel.com/ If a post is helpful please give it a vote of helpful
RadioListener  Tuesday, July 15, 2008 12:14 AM
Hi Steve,

1. You can read this KB article about using XmlDocument class to modify the XML file content. XML content can be classified broadly into a collection of nodes and attributes of the nodes. You can modify the content by modifying the nodes or attributes. The System.Xml.XmlDocument class implements the core XML DOM parser of the .NET Framework. This class is compliant with the World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1 and Level 2 Core standards. You can use the DOM model implementation to modify the content of XML documents; for example, you can insert, update, or delete data.

2. Validation. You can have a look at the XmlSchemaSet class. It is a cache or library where the XML Schema definition language (XSD) schemas can be stored. XmlSchemaSet improves performance by caching schemas in memory instead of accessing them from a file or a URL. Each schema in the XmlSchemaSet is identified by the namespace URI and schema location specified when then schema was added to the XmlSchemaSet. The Schemas property assigns the XmlSchemaSet object to use.

Best regards,
Riquel

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Riquel_Dong  Wednesday, July 16, 2008 7:27 AM

You can use google to search for other answers

Custom Search

More Threads

• Re: VS 2005 - Windows Service Project and Setup Project questions
• [OTP]Could I use Excel.Application in the Excel library to control the Excel application in the ASP.Net?
• Is there a way to access local variables from another sub/function?
• Delete files except the ones which are protected
• TreeView refresh
• Unicode/Ascii/etc... problems in vb.net application
• Creating a custom menu class and giving methods to its options?
• Showing the form on the position of the mouse
• Problem with SQL
• make exe. file with vb 2005