Results 1 to 8 of 8
  1. #1
    nathanael890's Avatar
    Join Date
    Aug 2008
    Gender
    male
    Posts
    158
    Reputation
    13
    Thanks
    46
    My Mood
    Bored

    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.


    Status: Unknown

  2. #2
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    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]
    Last edited by Hassan; 08-17-2011 at 10:59 AM.

  3. The Following User Says Thank You to Hassan For This Useful Post:

    nathanael890 (08-17-2011)

  4. #3
    nathanael890's Avatar
    Join Date
    Aug 2008
    Gender
    male
    Posts
    158
    Reputation
    13
    Thanks
    46
    My Mood
    Bored
    @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.
    Last edited by nathanael890; 08-17-2011 at 11:05 PM.


    Status: Unknown

  5. #4
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    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]

  6. The Following User Says Thank You to Hassan For This Useful Post:

    nathanael890 (08-18-2011)

  7. #5
    nathanael890's Avatar
    Join Date
    Aug 2008
    Gender
    male
    Posts
    158
    Reputation
    13
    Thanks
    46
    My Mood
    Bored
    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
    Last edited by nathanael890; 08-18-2011 at 05:18 AM.


    Status: Unknown

  8. #6
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    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]


  9. The Following User Says Thank You to Hassan For This Useful Post:

    nathanael890 (08-19-2011)

  10. #7
    nathanael890's Avatar
    Join Date
    Aug 2008
    Gender
    male
    Posts
    158
    Reputation
    13
    Thanks
    46
    My Mood
    Bored
    Oh Thanks. (Seems you're using VB2010. I don't know if its the same as 2008)

    Anyway, my problem is solved.


    Status: Unknown

  11. #8
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    Marked solved then. GG Hassan.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

Similar Threads

  1. [Help] Execute a .JAR file from VB. [Solved]
    By danishfps in forum Visual Basic Programming
    Replies: 14
    Last Post: 04-26-2011, 10:27 PM
  2. [Help]VB Read text file[Solved]
    By mo3ad001 in forum Visual Basic Programming
    Replies: 3
    Last Post: 06-16-2010, 03:49 AM
  3. TuT] How to Change File Attributes from Read only to Normal [TuT
    By XGelite in forum Visual Basic Programming
    Replies: 1
    Last Post: 11-19-2009, 01:08 PM
  4. Replies: 5
    Last Post: 07-22-2009, 04:26 PM
  5. Do You Know How To Read Or Convert Rez Files Into Somtin Else?
    By KillerKen013 in forum General Hacking
    Replies: 3
    Last Post: 05-19-2009, 02:20 PM