Reading XML Attributes From File [solved]

Posts 1–8 of 8 · Page 1 of 1
Reading XML Attributes From File [solved]
Hi guys. I need some help in reading XML files. I didn't even found a good result that can help me.

How can I get the values of the item inside a line of an XML file?

Example XML File:
Code:
<?xml version="1.0"?>
<Patchinfo RenameExe="false">
  <Files>
    <File Name="client.exe" Size="287937" Checksum="523de0b3" FileTime="0" />
  </Files>
</Patchinfo>
Let's say I want to get the information at the Files like this and put them in a textbox.
Code:
Filename: client.exe
Filesize: 287937 bytes
CheckSum: 523DE0B3
Filetime: 0
Btw. I'm making a program that can read the xml file to check the client files automatically.
Import the following namespaces:
[highlight=vbnet]Imports System.Linq
Imports System.Xml.Linq[/highlight]

Add Reference to these assemblies if required.

Then, use the following LINQ query to extract the information you need:

[highlight=vbnet]Dim LXML As XDocument = XDocument.Load("vv.xml")
Dim query = From c In LXML.Descendants("File")New With { _
Key .Name = c.Attribute("Name").Value, _
Key .Size = c.Attribute("Size").Value, _
Key .CheckSum = c.Attribute("Checksum").Value, _
Key .FileTime = c.Attribute("FileTime").Value _
}
For Each m As var In query
Msgbox(m.Name & Vbcrlf & m.Size ) ' ....
Next[/highlight]
@Hassan,
Thanks Hassan but there are some errors occured. -_-



1. End of statement expected (from the codes "New With" to "}")
2. Type var is not defined.
3. Name query is not declared

I'm using VB 2008 and still a newbie for VB programming.
Quote Originally Posted by nathanael890 View Post
@Hassan,
Thanks Hassan but there are some errors occured. -_-



1. End of statement expected (from the codes "New With" to "}")
2. Type var is not defined.
3. Name query is not declared

I'm using VB 2008 and still a newbie for VB programming.
Sorry, wrote it in C# and converted it. Anyways, have tested it now in VB.NET. Works Fine.

[highlight=vbnet]Dim LXML As XDocument = XDocument.Load("vv.xml")
Dim query = From c In LXML.Descendants("File") Select New With { _
Key .Name = c.Attribute("Name").Value, _
Key .Size = c.Attribute("Size").Value, _
Key .CheckSum = c.Attribute("Checksum").Value, _
Key .FileTime = c.Attribute("FileTime").Value _
}
For Each m As Object In query
MsgBox(m.Name & vbCrLf & m.Size) ' ....
Next[/highlight]
Thanks Hassan. It works for me.

Edit: I got an error when trying to put it in a listbox (temporarily)


I would now like to add them to a listview for organizing it.
@Hassan
Quote Originally Posted by nathanael890 View Post
Thanks Hassan. It works for me.

Edit: I got an error when trying to put it in a listbox (temporarily)


I would now like to add them to a listview for organizing it.
@Hassan
I think the error occurs because it couldn't find an attribute as it occurred when I deleted an attribute. But not really sure, if that's the only reason. You should put some check on whether all the attributes are present. Anyways, here's how you display it in ListView. First add a listview, then setup its Columns as shown in the following image:



Here's the updated ForEach code:
[highlight=vbnet]For Each m As Object In query
ListView1.Items.Add(m.Name)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(m.Size)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(m.CheckSum)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(m.FileTime)
Next[/highlight]

Oh Thanks. (Seems you're using VB2010. I don't know if its the same as 2008)

Anyway, my problem is solved.
Marked solved then. GG Hassan.
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us