locked
taking a value of an attribute from an xml node RRS feed

  • Question

  • Hi everybody,

    I have created an xml file and i would like to now extract data from it.

    Is there a function within vb.net that reads xml and takes out all the attributes with a certain name? For example to withdraw all the authors names from an xml file about books?

     

    Cordialement,

    Miller


    If you're not living on the edge, you're taking up too much room
    Thursday, March 25, 2010 2:40 PM

Answers

  • Hi

    In vb.net load your xml into a string( e.g. XMLString) - you can then loop through all the xmlAuthors.

     

    Dim objxml As New XmlDocument
    objxml.LoadXml(XmlString) 
    Dim xmlAuthors As XmlNodeList = xmlDoc.SelectNodes("//Books/Book/Author/AuthorName")
    For i as integer = 0 to xmlAuthors.count-1
    Dim AuthorName as string = xmlAuthors(i).InnerText
    Next

    It would help to see an example of the xml

    regards

     

    John

    • Marked as answer by Martin_Xie Thursday, April 1, 2010 5:33 AM
    Thursday, March 25, 2010 3:15 PM

All replies

  • Hi

    In vb.net load your xml into a string( e.g. XMLString) - you can then loop through all the xmlAuthors.

     

    Dim objxml As New XmlDocument
    objxml.LoadXml(XmlString) 
    Dim xmlAuthors As XmlNodeList = xmlDoc.SelectNodes("//Books/Book/Author/AuthorName")
    For i as integer = 0 to xmlAuthors.count-1
    Dim AuthorName as string = xmlAuthors(i).InnerText
    Next

    It would help to see an example of the xml

    regards

     

    John

    • Marked as answer by Martin_Xie Thursday, April 1, 2010 5:33 AM
    Thursday, March 25, 2010 3:15 PM
  • If you are targetting .NET 3.5, using Linq to XML provides an easier way to do this.

    I have an example of finding a node here:

    http://msmvps.com/blogs/deborahk/archive/2010/01/31/finding-a-node-in-an-xml-string.aspx

    If you want all of the nodes that match, just remove the "FirstOrDefault" from the end of the command in the example.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Thursday, March 25, 2010 3:36 PM
  • Just remembered, last time you posted you were using VS 2005? if that is the case, you won't be able to use Linq to XML. You'll have to go with John's technique. Or upgrade? :-)
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Thursday, March 25, 2010 4:55 PM
  • I am using VS 2005. So i will use Johns example thank you for the new technique though Deborah. When i upgrade i will use that.

    An example of my xml code is below i used the example of books as that is what microsoft seems to use in the xml example projects. I have an xml file that contains a specification to draw a table. I just need to upgrade code from vb6 to .net on how to draw the tables from this. 

    <?xml version="1.0" encoding="UTF-8"?>
    <table code=" " tabstyle="RA" frame="all">
    <title>MINIMUM V2 (KT IAS)</title>
    <tgroup cols="2" colsep="1" rowsep="1" tabstyle="column band">
    <colspec colname="col1" colwidth="2cm" />
    <colspec colname="col2" colwidth="1.5cm" />
    <thead>
    <row>
    <entry namest="col1" nameend="col2" align="center" valign="middle">
    <paratext>MINIMUM V2 (KT IAS)</paratext>
    </entry>
    </row>
    </thead>
    <tbody>
    <row>
    <entry colname="col1" morerows="1" rowsep="1" align="center" valign="middle">
    <row-title>CONF</row-title>
    </entry>
    <entry namest="col2" nameend="col11" align="center" valign="middle">
    <col-title>PRESSURE ALTITUDE (FT)</col-title>
    </entry>
    </row>
    <row>
    <entry colname="col2" align="center" valign="middle">
    <col-value>-2000</col-value>
    </entry>
    </row>
    </tgroup>
    <tgroup cols="2" colsep="1" rowsep="1" tabstyle="tperfo body">
    <colspec colname="col1" colwidth="2cm" />
    <colspec colname="col2" colwidth="1.5cm" />
    <tbody>
    <row>
    <entry colname="col1" align="center" valign="middle">
    <abscissa-value>1+F</abscissa-value>
    </entry>
    <entry colname="col2" rowsep="1" align="center" valign="middle">
    <perfo-value split="false">114</perfo-value>
    </entry>
    </row>
    <row>
    <entry colname="col1" align="center" valign="middle">
    <abscissa-value>2</abscissa-value>
    </entry>
    <entry colname="col2" rowsep="1" align="center" valign="middle">
    <perfo-value split="false">114</perfo-value>
    </entry>
    </row>
    <row>
    <entry colname="col1" align="center" valign="middle">
    <abscissa-value>3</abscissa-value>
    </entry>
    <entry colname="col2" rowsep="1" align="center" valign="middle">
    <perfo-value split="false">114</perfo-value>
    </entry>
    </row>
    </tbody>
    </tgroup>
    <source>AB201A01 A330-201 OCTO27.0.0</source>
    </table>

     


    If you're not living on the edge, you're taking up too much room
    Friday, March 26, 2010 3:23 PM