Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi, just wondered if anyone could help. I'm new to VB .NET and XML but I'm 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 I've done a search but getting no where, every example shown uses different methods and I'm now totally lost.

 

Hope someone can point me in the right direction, hopefully with an example, regards Muse

Posted

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 it's slower and requires alot of system resources, especially with large xml files. Heres a quick example of how to use the XmlTextReader.

        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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

Hi, thanks for your reply but it doesn't 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 idea's

Posted

Are you loading the text into the first textbox yourself? If you are then you don't need to as its much easier to work with the text as an xml file. If your not then you have two options. There's an overload for the xmltextreader that works with a string rather than an xml file, however I've 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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

Hi, maybe I'm not explaining this c;early, the XML information is being grapped using a schema (I think) told you I'm 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

Posted

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 I've had a look at the overloads, the following might work, but I haven't tested it.

'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)

Anybody looking for a graduate programmer (Midlands, England)?
Posted
Unless you can provide the exact string of xml you are attempting to parse, I can't really help you any further. It is very difficult for me to run tests without having the same data.
Anybody looking for a graduate programmer (Midlands, England)?
Posted

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 I'm back to invalid characters in path, the value of stringReader is equal to the XML value !

Posted

I'm not surprised it doesn't 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.

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)

Anybody looking for a graduate programmer (Midlands, England)?
Posted
Sorry but now I'm totally confused, the example you just gave returns an error statin that the stringreader is already defined, which I've then rem'd but I can't figure out now how to amend the original code you gave to actually get the surname, forename etc. help
Posted

Full code is here, I have no idea if it will work correctly though as I don't know exactly what XML contains.

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

Anybody looking for a graduate programmer (Midlands, England)?
  • *Experts*
Posted

I'd 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. I've attached the whole C# project as well.

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 isn't valid - probably not cut and paste, but make sure the string IS valid XML.

 

For example, your string had a comma between elements which isn't 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 it's 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

XMLTest.zip

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

I've never really liked the Xml Document, I've always found it to be alot slower and very system resource intensive. This isn't 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.

Anybody looking for a graduate programmer (Midlands, England)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...