New to .net and XML, help

Muse

Member
Joined
Feb 11, 2006
Messages
6
Hi, just wondered if anyone could help. Im new to VB .NET and XML but Im loading an XML file into a text box and then I want to extract the information into various text fields.

i.e in the XML file I have the <usersname>????</username>, <surname>???</surname>, <house>?????</house>,<street> etc and I want to get the values held in these and others.

Tried various examples shown when Ive done a search but getting no where, every example shown uses different methods and Im now totally lost.

Hope someone can point me in the right direction, hopefully with an example, regards Muse
 
The two common methods I know of are using the XML DOM (document object model) and the XmlTextReader. Using the DOM can be alot easier to understand but its slower and requires alot of system resources, especially with large xml files. Heres a quick example of how to use the XmlTextReader.
Code:
        Dim path As String = "c:\myfile"
        Dim reader As New XmlTextReader(path)

        While reader.Read()
            If reader.NodeType = XmlNodeType.Element Then
                If reader.Name = "usersname" Then
                    usernameTextBox.Text = reader.ReadInnerXml()
                ElseIf reader.Name = "surname" Then
                    surnameTextBox.Text = reader.ReadInnerXml()
                End If
            End If
        End While

        by the sounds of it your xml is being used as storage for a collection, 
        if this is the case you will first have to navigate to the right item
        using similar methods to those shown here.
NB. Code not tested, might be a few bugs.
 
Hi, thanks for your reply but it doesnt seem to work, I think the first problem is the xml file is already loaded into a text field, txtResult.text.

Using your method I get a system.ArgumentException, stating the path as invalid characters in the path

Any ideas
 
Are you loading the text into the first textbox yourself? If you are then you dont need to as its much easier to work with the text as an xml file. If your not then you have two options. Theres an overload for the xmltextreader that works with a string rather than an xml file, however Ive never personally used it. Alternatively you could write the contents of the initial textbox to a temporary file, thus allowing you to use the overload I suggested.

If you post the exact code that produced the error message you have provided I might be able to help further.
 
Hi, maybe Im not explaining this c;early, the XML information is being grapped using a schema (I think) told you Im not that good on this, but my program ends up with the field XML equal to the contents on the XML file, I was then putting this into the txtResult.text richtextbox.

SO thats where I am and now I need to extract certain information based on the client, i.e. their forename, surname, address details etc.

AMended your code;

Dim path As String = XML
Dim reader As New XmlTextReader(path)

While reader.Read()
If reader.NodeType = XmlNodeType.Element Then
If reader.Name = "FamilyName" Then
txtSurname.Text = reader.ReadInnerXml()
End If
End If
End While

but get the invalid characters in path, where path equals the data found in XML, would I be better to use some kinfd of search for string method, if so could you help on that also, regards Muse
 
It would produce an error, the overload I specified requires a path, what you are passing it is not a path, it is a string of xml data (and thus contains invalid path chars such as < and >). One of the other overloads can work with a string but it requires extra parameters and gets quite complicated.

Basically it sounds like your in a situation where you wish to parse a string object as though its xml. I personally know of no easy way of doing this without saving it to disk first or writing your own parser. What I was trying to get at in my previous post was that perhaps theres a better way to get the xml in the first place rather than ending up with it as a string.

Having said all this Ive had a look at the overloads, the following might work, but I havent tested it.
Code:
load your xml fragment / string into a string reader object
Dim stringReader As New System.IO.StringReader(XML)
then use the overload that excepts the single parameter TextReader
Dim xmlReader As New XmlTextReader(stringReader)
 
Unless you can provide the exact string of xml you are attempting to parse, I cant really help you any further. It is very difficult for me to run tests without having the same data.
 
I imported the system bit;

Imports System.IO.StringReader

Then amended your code to;

load your xml fragment / string into a string reader object
Dim stringReader As New String(XML)

then use the overload that excepts the single parameter TextReader
Dim xmlReader As New XmlTextReader(stringReader)

but then Im back to invalid characters in path, the value of stringReader is equal to the XML value !
 
Im not surprised it doesnt work you have passed it another string not a stringreader, and as such it assumes its a path. Your amendment of my code completely changed the type of object being passed. If you wished to shorten the code in the project it should have been like this.
Code:
Imports System.IO
 the code needs to create an instance of a stringreader
Dim stringReader As New StringReader(XML)
 your code creates an instance of a string
Dim stringReader As New String(XML)
 
Sorry but now Im totally confused, the example you just gave returns an error statin that the stringreader is already defined, which Ive then remd but I cant figure out now how to amend the original code you gave to actually get the surname, forename etc. help
 
Full code is here, I have no idea if it will work correctly though as I dont know exactly what XML contains.
Code:
Imports System.IO
Imports System.Xml
     
Dim myStringReader As New StringReader(Xml)
Dim myXmlReader As New XmlTextReader(myStringReader)

While myXmlReader.Read()
      If myXmlReader.NodeType = XmlNodeType.Element Then
          If myXmlReader.Name = "usersname" Then
               usernameTextBox.Text = myXmlReader.ReadInnerXml()
          ElseIf myXmlReader .Name = "surname" Then
               surnameTextBox.Text = myXmlReader.ReadInnerXml()
          End If
      End If
End While
 
Id use an XmlDocument to load the string. You can use XPath syntax to select out the elements you want. The two most useful methods are SelectNodes and SelectSingleNode. Below is a code snippet. Ive attached the whole C# project as well.
C#:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(txtXML.Text);

XmlNode usersname = xmlDoc.SelectSingleNode("/TestRoot/usersname");
if (usersname != null)
{
	txtusersname.Text = usersname.InnerText;
}

XmlNode surname = xmlDoc.SelectSingleNode("/TestRoot/surname");
if (surname != null)
{
	txtsurname.Text = surname.InnerText;
}

I hope you understand that to do anything with XML, you must have valid XML. Your original sample string isnt valid - probably not cut and paste, but make sure the string IS valid XML.

For example, your string had a comma between elements which isnt allowed. It also had an element "usersname" with a closing element of "username" - hopefully just a typo.

If you just have a bunch of separate elements surrounded by commas, then its CRITICAL that you tell us that. As Cags asked, a real sample string would help us to help you much better - you can fake the values if you want, but the formatting of the elements and such is very important.

-ner
 

Attachments

Ive never really liked the Xml Document, Ive always found it to be alot slower and very system resource intensive. This isnt really going to matter with smaller pieces of xml, but with large files it can literally cripple a PC.

As Nerseus has shown however it is still a valid alternative, and can lead to easier to understand code.

NB. Some of these issues may have been addressed in .Net 2.0, my comments are based on .Net 1.1.
 
Back
Top