• 0

[VB.NET] XML Parsing into dynamic controls


Question

I see a lot of people here are trying to help each other out with the XML parsing issues. I can connect to databases just fine, but connecting to an XML file is killing me. Let me give a rundown of what I'm trying to achieve.

The XML file contains:

<?xml version="1.0"?>
<data>

 ?<question id="1">
 ? ?<text>What is your sex?</text>
 ? ?<choices>
 ? ? ?<choice>Male</choice>
 ? ? ?<choice>Female</choice>
 ? ?</choices>
 ?</question>

and so on..

Basically, the user has to take a survey. The questions will be read from an xml file, with the question itself being assigned to a label. Next, the possible answer choices are read and are assigned to radio buttons. I've attached a picture of the form to help other people get:Dhe picture :D

Once I can get this connected, I could make the parsing recursive such that it keeps showing questions till the xml file is complete. I'd appreciate a point in the right direction.

At the moment, I am having absolutely no luck when it comes to getting connected>connected and I am going crazy. Let alone the fact that I have to later write back the user response to a different XML file...'Tis the life:no:an intern :no:

post-47-1089748788.jpg

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

You're gonna have to look into XmlDocument or parsing out the xml file yourself using XmlNodes and what not. And then basically your gonna have to write a parsing engine to convert a <choice> into a checkbox/radiobutton, should be fun :)

Link to comment
Share on other sites

  • 0

Why not just load you XML file into a dataset, you could then simply display questions and answers, update the dataset and then write it back to the XML file !

This will read your file into the dataset . . .

Dim ds As New DataSet
ds.ReadXml("data.xml")

and this will write dataset data back to the XML file, if you change the name of the file in the example below then this will write a new XML file . . .

ds.WriteXml("data.xml")

Using XML combined with datasets, you could easily work out a percentage score display at the end of the survey !

Kind Regards

Link to comment
Share on other sites

  • 0

I had tried the dataset method before, but I get an error everytime. I have imported "Imports System.Xml" at the top of the form, but the program execution stops at the ReadXML line.

I get the error:

An unhandled exception of type 'System.Xml.XmlException' occurred in system.xml.dll

Additional information: System error.

I have placed the xml file in both the bin folder and the debug folder :o

Link to comment
Share on other sites

  • 0

go to msdn.com

search for DOM, XPath, XMLDocument,XPathdocument

//example1

Imports System.Xml

Imports System.Xml.XPath

Dim Doc As XPathDocument = New XPathDocument("emp.xml")

Dim Navigator As XPathNavigator

Navigator = Doc.CreateNavigator()

Dim Iterator As XPathNodeIterator = Navigator.Select("/employees/employee")

While Iterator.MoveNext()

Console.WriteLine(Iterator.Current.Name)

Console.WriteLine(Iterator.Current.Value)

End While

//example2 C# but their is a similar vb alternative

XmlDocument doc = new XmlDocument();

doc.LoadXml(xString);

XmlDocument doc_Schema = new XmlDocument();

doc_Schema.LoadXml(xSchema);

XmlNodeList nodeList;

XmlNode nodeSchema;

XmlElement root = doc.DocumentElement;

XmlElement rootSchema=doc_Schema.DocumentElement;

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");

nodeList = root.SelectNodes("/InfoDataSet/Table/*");//xpath querry

foreach (XmlNode isbn in nodeList)

{

console.Write(isbn.InnerText);

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.