XMLDocument in VB.NET

Get help on programming - C++, Java, Delphi, etc.
Post Reply
Screeper
Registered User
Posts: 3692
Joined: 04 Apr 2003, 02:00
Contact:

XMLDocument in VB.NET

Post by Screeper »

Afternoon all,

Am grappling with a task at the moment and was wondering if anyone has had any experience with the XMLDocument class in VB.NET?

I need to read an XML document and extract just the elements and display them in a textbox.
The catch is the XML documents are random so I need to use (I think) the XMLDocument class to run through the XML doc and pull all the elements out as it encounters them. I suppose I could use XMLReader but I have a feeling that the reader may end up being a bit restrictive later on in development.

Below is a snippet of the xml I will be (attempting) reading. So in this case i would need to get the 'SO' and the 'O' from the code and put them in the textbox.

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?> 
- <LAIBATCH>
-   <LAIMSG version="1.0" name="salesorder">
-     <REC>
        <TYP>SO</TYP> 
        <ACT>O</ACT> 
      </REC>
    </LAIMSG>
  </LAIBATCH>
Anyone got any insight into this? Even if it is just to point me towards a useful website.
There are 10 types of people in this world.
Those who understand binary and those who do not.
Kronos
Moderator Emeritus
Posts: 4280
Joined: 28 May 2003, 02:00
Location: Azeroth
Contact:

Re: XMLDocument in VB.NET

Post by Kronos »

What do you mean by "the XML documents are random"?
What is random in the document? The elements? Order of the elements? depth of the elements?

You can use the XMLDocument class yes. But I don't really see what your actual question is. :?
Image
Screeper
Registered User
Posts: 3692
Joined: 04 Apr 2003, 02:00
Contact:

Re: XMLDocument in VB.NET

Post by Screeper »

Random as in the XML docs will not be the same each time. Elements may have different names, attributes may differ, depths of elements will differ etc..

Question - Is the XMLDocument class the correct class to be doing the job of sifting through each XML doc?
And if there is any code or anything that you know about that may help, as the MSDN and the Offline help give 2 examples that are so involved I end up more confused than ever.
There are 10 types of people in this world.
Those who understand binary and those who do not.
Kronos
Moderator Emeritus
Posts: 4280
Joined: 28 May 2003, 02:00
Location: Azeroth
Contact:

Re: XMLDocument in VB.NET

Post by Kronos »

Ah, ok. this is a bit more tricky since the element names may differ.

However, it can be done using the XMLDocument class.

Here is a demo, but note that I am making the following assumptions:
1. There is always a document element
2. The Values you want to display are always at the leaf node of the XML

I hope you can make sense of C# code, since I don't know VB

You need to write a recursive Function that will loop through the nodes of the XMLDocument given the root element of the XMLDocument
Like so:

Code: Select all

private void GetValue(XmlElement root)
{
   //Loop through the childnodes of the given root
   foreach(XmlNode n in root.ChildNodes)
   {
      //If there are no child nodes, then this is the leaf node.
      if(!n.HasChildNodes)
      {
         //Add the value to the textbox
         textbox1.Text += n.InnerText;
      }
      else
      {
         //Recurse this function to get to the child nodes and eventually to the Leaf node
         GetValue(n);
      }
   }
}
Then you can call it from the loading function as follows:

Code: Select all

//Create the XMLDocument Object
XMLDocument doc = new XMLDocument();
//Load the XML Document
doc.Load(pathToFile);

//Call the recursive function to get the values from the document
GetValue(doc.DocumentElement);
Image
Post Reply