Stuggling with XML.....WHY?

hog

Well-known member
Joined
Mar 17, 2003
Messages
984
Location
UK
OK, for some reason this XML stuff eludes me. Im trying to do the following as an exercise in learning it.

I have an xml file with this content:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
<CATALOG>
	<CD>
		<TITLE>Empire Burlesque</TITLE>
		<ARTIST>Bob Dylan</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Columbia</COMPANY>
		<PRICE>10.90</PRICE>
		<YEAR>1985</YEAR>
	</CD>
	<CD>
		<TITLE>Hide your heart</TITLE>
		<ARTIST>Bonnie Tyler</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>CBS Records</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1988</YEAR>
	</CD>
	<CD>
		<TITLE>Greatest Hits</TITLE>
		<ARTIST>Dolly Parton</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>RCA</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1982</YEAR>
	</CD>
</CATALOG>

All I want to do is find out how to access the elements that hold the data. Im tried the DOM and Xpath and now xmlTextReader. I have got further with the latter but it all seems messy.

Here is some code:

Code:
    Dim xReader As Xml.XmlTextReader = New Xml.XmlTextReader("C:\Documents and Settings\paul\My Documents\Visual Studio Projects\SampleSchema\cd_catalog.xml")

        Dim htCDCollection As Hashtable = New Hashtable

        Dim strNodeName As String

        Dim intCounter As Integer

        Do While xReader.Read

            If xReader.NodeType = XmlNodeType.Element Then

                htCDCollection.Add(xReader.Name & intCounter, Nothing)
                strNodeName = xReader.Name & intCounter
                Me.rtbCDCollection.Text = Me.rtbCDCollection.Text & xReader.Name & vbCrLf & vbTab

            End If

            If xReader.NodeType = XmlNodeType.Text Then

                htCDCollection.Item(strNodeName) = xReader.Value
                Me.rtbCDCollection.Text = Me.rtbCDCollection.Text & xReader.Value & vbCrLf

            End If

            intCounter += 1

        Loop

        xReader.Close()
        xReader = Nothing

The hashtable is there as Im trying to figure out a way to store the data so I can move back and forth through it.

All help gratefully recieved.

Thnx:)
 
I bought a eBook today, Visual Basic .Net and XML, which started out well in chapter 1 but just rambles on with loads of text and gets mighty confusing re schemas.

Does anyone know of any text that speaks plain English for the complete idiot? I am baffled as to why this is leaving me stumped??
 
If it were me, Id use the XmlDocument object to read in the file. You can use XPath query syntax to locate nodes by calling the selectSingleNode or selectNodes methods. You can read the attributes (you have none right now) using the Attributes property and its GetNamedItem method.

Heres some C# code (should be easy to translate - ignore the @ for VB):
C#:
XmlDocument d = new XmlDocument();
d.Load(@"c:\temp\xml.xml");
XmlNode cdNode = d.SelectSingleNode(@"/*/CD[TITLE=Hide your heart]");
if(cdNode!=null)
{
	XmlNode artist = cdNode.SelectSingleNode("ARTIST");
	if(artist!=null) Debug.WriteLine(artist.InnerText);
	XmlNode price = cdNode.SelectSingleNode("PRICE");
	if(price!=null) Debug.WriteLine(price.InnerText);
	artist.Attributes.GetNamedItem
}

-Nerseus
 
Okey dokey.....

I now have this code:

Code:
    Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click

        Dim xDocument As New XmlDocument

        xDocument.PreserveWhitespace = False

        xDocument.Load("C:\Documents and Settings\paul\My Documents\Visual Studio Projects\SampleSchema\cd_catalog.xml")

        Me.rtbCDCollection.Text = DisplayNodeInformation(xDocument)

    End Sub

    Public Function DisplayNodeInformation(ByVal xNode As XmlNode, Optional ByVal intIndent As Integer = 0) As String

        Dim strOutput As String
        Dim xChildNode As XmlNode

        If xNode.NodeType = XmlNodeType.Text Then

            strOutput = Space(intIndent) & xNode.Value & ControlChars.CrLf

        Else

            strOutput = Space(intIndent) & xNode.Name & ControlChars.CrLf

        End If


        For Each xChildNode In xNode.ChildNodes

            strOutput = strOutput & DisplayNodeInformation(xChildNode, intIndent + 4)

        Next

        Return strOutput

    End Function

which produces this:

#document
xml
#comment
CATALOG
CD
TITLE
Empire Burlesque
ARTIST
Bob Dylan
COUNTRY
USA
COMPANY
Columbia
PRICE
10.90
YEAR
1985
CD
TITLE
Hide your heart
ARTIST
Bonnie Tyler
COUNTRY
UK
COMPANY
CBS Records
PRICE
9.90
YEAR
1988
CD
TITLE
Greatest Hits
ARTIST
Dolly Parton
COUNTRY
USA
COMPANY
RCA
PRICE
9.90
YEAR
1982
CATALOG

Im starting to get this xml stuff but am still struggling to work my way around a document.

A few questions....

Am I correct in assuming the program needs to know the format of the expected xml file? Ive being working on the idea that it does not so it has to go through all this checking stuff to confirm the format??

Is it better to read the whole xml file into a dataset structure and manipulate the data from there and if so any pointers?

Thnx :)
 
Mmm .NET Samples - How To: XML Data gives a long list of examples that can be picked apart and stepped through to see what is happening.

Slowly.....very slowly its starting to gel :)
 
Am I correct in assuming the program needs to know the format of the expected xml file? Ive being working on the idea that it does not so it has to go through all this checking stuff to confirm the format??
XML is whatever you want it to be - seriously. If the XML is coming from you (from code you write) then you probably dont need a schema (to do semi-automatic validation) or any custom validation as you should know what youre generating. This would be the case, for instance, if you have a method that returns XML or youre getting the XML from a DataSet.

If youre receiving data from an outside source then you cant be sure its "good" XML (might be valid XML syntax, but missing required elements for instance). In that case its best to use a Schema. A schema, in short, is an XML file that contains information on what another XML file should look like. It would list required elements and attributes, optional els and atts, the number of rows that can/should/must be in each element, datatypes of elements and more. Through a few lines of code you can use the built in validating objects to quickly tell if an XML file matches the schema and get a simple yes/no answer.

Is it better to read the whole xml file into a dataset structure and manipulate the data from there and if so any pointers?
Assuming youre receiving XML FROM a DataSet, you can easily read it back into a DataSet. If its not a DataSet I wouldnt try to load it into one. Id stick with the XmlDocument object. There are methods to find nodes, node lists (including filtered lists), get the attributes and values and more.

Now if you have a DataSet, there is a special XML object called XmlDataDocument. Its an XmlDocument object that binds to a DataSet and keeps itself in sync. So if you find a node in the XML and update a text value, for instance, it will change the value in the DataSet as well. Likewise, changing a value in the DataSet will show up immediately in the XML object.

-Ner
 
XML is whatever you want it to be - seriously. If the XML is coming from you (from code you write) then you probably dont need a schema (to do semi-automatic validation) or any custom validation as you should know what youre generating. This would be the case, for instance, if you have a method that returns XML or youre getting the XML from a DataSet.

Aha makes perfect sense and will make life easier :)

If youre receiving data from an outside source then you cant be sure its "good" XML (might be valid XML syntax, but missing required elements for instance). In that case its best to use a Schema. A schema, in short, is an XML file that contains information on what another XML file should look like. It would list required elements and attributes, optional els and atts, the number of rows that can/should/must be in each element, datatypes of elements and more. Through a few lines of code you can use the built in validating objects to quickly tell if an XML file matches the schema and get a simple yes/no answer.

Again, perfect sense but need to suss out how to use a schema to validate an xml file.

Im still searching for a good book that works from the basics up with lots of examples. The few I have obtained from Amazon are crap as they just ramble on for pages with the occasional example few and far between. I am starting to get it very slowly but the info is too spread out:(

Normally I get hold of one or two good books and dive in until I get it.........you can see this just aint happening with xml.

What makes it more annoying is that everywhere I read how easy it is to use but am yet to find the right tutorial to prove this to be the case :( :(

I have this code which I have mdified from the VS Help which is the first real bit of code I have managed to actually get to do anything half way descent with xml.

Code:
   Const strDocument As String = "C:\Documents and Settings\paul\My Documents\Visual Studio Projects\xmlTest1\cd_catalog.xml"

        Dim xPathDocument As XPathDocument = New XPathDocument(strDocument)

        Dim xPathNav As XPathNavigator = xPathDocument.CreateNavigator

        xPathNav.MoveToRoot()

        Dim xPathNodeIterate As XPathNodeIterator = xPathNav.Select("descendant::CD/TITLE")

        Dim intCount As Integer = 0

        While xPathNodeIterate.MoveNext

            intCount += 1

            Me.rtbCDCollection.Text &= intCount & "   <" & xPathNodeIterate.Current.Name & ">" & _
                                      xPathNodeIterate.Current.Value & ControlChars.CrLf

        End While

The xPathNav.Select method above references I presume is the namespace descendant. Is this a default namespace name in all xml files? I understand that this method is selecting the TITLE child element of the CD element in the descendant namespace.

Addition:

Just got this to work. Is this the normal way to pick out valuse from an xml file?

Code:
        Const strDocument As String = "C:\Documents and Settings\paul\My Documents\Visual Studio Projects\xmlTest1\xconfig.xml"

        Dim xPathDocument As XPathDocument = New XPathDocument(strDocument)

        Dim xPathNav As XPathNavigator = xPathDocument.CreateNavigator

        xPathNav.MoveToRoot()

        Dim xPathNodeIterate As XPathNodeIterator = xPathNav.Select("descendant::config/version")

        xPathNodeIterate.MoveNext()

        Dim strVersion As String = xPathNodeIterate.Current.Value

        xPathNodeIterate = xPathNav.Select("descendant::config/released")

        xPathNodeIterate.MoveNext()

        Dim strReleased As String = xPathNodeIterate.Current.Value

        Me.rtbCDCollection.Text = "The lastest version of " & strVersion & " was released on " & strReleased
 
Last edited by a moderator:
Back
Top