Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

OK, for some reason this XML stuff eludes me. I'm trying to do the following as an exercise in learning it.

 

I have an xml file with this content:

 


<?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. I'm tried the DOM and Xpath and now xmlTextReader. I have got further with the latter but it all seems messy.

 

Here is some 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 I'm trying to figure out a way to store the data so I can move back and forth through it.

 

All help gratefully recieved.

 

Thnx:)

My website
Posted

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??

My website
  • *Experts*
Posted

If it were me, I'd 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.

 

Here's some C# code (should be easy to translate - ignore the @ for VB):

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

"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

Okey dokey.....

 

I now have this 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

 

 

I'm 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? I've 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 :)

My website
Posted

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 it's starting to gel :)

My website
  • *Experts*
Posted

Am I correct in assuming the program needs to know the format of the expected xml file? I've 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 don't need a schema (to do semi-automatic validation) or any custom validation as you should know what you're generating. This would be the case, for instance, if you have a method that returns XML or you're getting the XML from a DataSet.

 

If you're receiving data from an outside source then you can't be sure it's "good" XML (might be valid XML syntax, but missing required elements for instance). In that case it's 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 you're receiving XML FROM a DataSet, you can easily read it back into a DataSet. If it's not a DataSet I wouldn't try to load it into one. I'd 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. It's 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

"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 (edited)

 

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

 

 

Aha makes perfect sense and will make life easier :)

 

 

If you're receiving data from an outside source then you can't be sure it's "good" XML (might be valid XML syntax, but missing required elements for instance). In that case it's 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.

 

I'm 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 ain't 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.

 


  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?

 


       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

Edited by hog
My website

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...