Help with XML in dotNet

mancombseepgood

Executive Member
Joined
Jun 1, 2004
Messages
9,351
Reaction score
2
Location
.
I have a simple XML document:

---
Code:
<?xml version="1.0" encoding="utf-8"?>
<Installations Current="Third">
  <Set Name="Default">
    <MaintCnt>10</MaintCnt>
    <Location>C:\Installation\Default</Location>
    <Lastuser />
    <Version>Unknown</Version>
  </Set>
  <Set Name="Second">
    <MaintCnt>9</MaintCnt>
    <Location>C:\Installation\Second</Location>
    <Lastuser />
    <Version>2.003</Version>
  </Set>
  <Set Name="Third">
    <MaintCnt>8</MaintCnt>
    <Location>C:\Installation\Third</Location>
    <Lastuser />
    <Version>1.306</Version>
  </Set>
</Installations>
---

I am using VB to read a single element called Set where the 'Name'
attribute = 'Second'.
My code is as follows:

---
Code:
Dim doc As XmlDocument = New XmlDocument()
doc.Load("C:\Installation\install.xml")
Dim setnm As XmlNode
setnm = doc.SelectSingleNode("/Installations/Set[@Name= Second ]")
---

The above code never returns anything... what am I missing?
Thanks :)
 
Your XPath expression is wrong.

The following will work:

Code:
setnm = doc.SelectSingleNode("/Installations/Set[@Name='Second']")
 
Awesome - tanks Gnome - great link... XML baffles me at times... need to spend more time reading methinks!
 
Why don't you de-serialize the XML into an object work with it and when you're done with it, you serailize it again to disk?

Works a lot easier and faster! If you do it this way you don't have to worry about XPaths and those kind of things. One thing you have to keep in mid though is that you XML file's structure should stay the same, if you change the structure the serialization would fail.
 
Thanks MielieSpoor - any suggestions or examples given my XML structure above?
:)
 
Why don't you de-serialize the XML into an object work with it and when you're done with it, you serailize it again to disk?

Works a lot easier and faster! If you do it this way you don't have to worry about XPaths and those kind of things. One thing you have to keep in mid though is that you XML file's structure should stay the same, if you change the structure the serialization would fail.

Have you actually done performance testing (IE. Disk/Memory/CPU usage, etc.)? Because serializing the data doesn't seem very fast to me, string handling is some of the most expensive operations, which you'll be doing x2 (convert from a string to a object types and then back to strings), not to mention the huge amount of IO operations of serializing the entire document, also sounds like a huge memory footprint.

Obviously there are situations that are faster using this method (IE. heavy XML transformations) but just seems overkill for reading from a document.

Btw. XPath is a handy thing to know (in fact XSL in general is very very handy).

Just my 2c.
 
Top
Sign up to the MyBroadband newsletter
X