Read xml using vb.net and store contents in a text file

Hi,
   I've an xml file  and i'm trying to read the data and store its contents into .txt file with a specified format. My problem is that i don't want to read all the attributes in that xml file, only certain values. I tried using the reader.AttributeCount and also used MoveToAttribute(index) but somehow I'm ending up with the data i don't need! Can anyone help me with this. Thank you in advance,

Needed values:
1. <nt ntyp="ekv" le="GHI09"></nt>
2. <nt ntyp="ptv" le="1239"></nt>
3. <Win typ="98"></Win>
4. <price>895</price> where country = "IN"
5. <text sp="IT" art="SSS">Windows Operating System</text>


XML file:

<?xml version = "1.0">
<pt>
   <nt ntyp="ekv" le="GHI09"></nt>
   <nt ntyp="esv" le="JLK32"></nt>
   <nt ntyp="ptv" le="1239"></nt>
   <Win typ="98"></Win>
</pt>
<price_pt>
<Country name="DA">
  <stTag size="421765">
    <price>965</price>
    <Curr>$</Curr>
  </stTag>
</Country>
<Country name="IN">
  <stTag size="458765">
    <price>895</price>
    <Curr>RS</Curr>
  </stTag>
</Country>
<Country name="TA">
  <stTag size="68765">
    <price>655</price>
    <Curr>RP</Curr>
  </stTag>
</Country>
<price_pt>
<service code>
  <service_log>N</service_log>
</service code>
<text_part>
<text sp="IN" art="VSS">WinXP</text>
<text sp="IT" art="SSS">Windows Operating System</text>
</text_part>
krishna_tmAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
zulu_11Connect With a Mentor Commented:
ok! here a sample vb code which reads the xml file and then shows u the price of the country whose name is IN
using Xpath...
similarly u can use Xpath to get whaterver values u want...

-------XML FILE (i used)

<?xml version = "1.0"?>
<root>
<pt>
   <nt ntyp="ekv" le="GHI09"></nt>
   <nt ntyp="esv" le="JLK32"></nt>
   <nt ntyp="ptv" le="1239"></nt>
   <Win typ="98"></Win>
</pt>
<price_pt>
<Country name="DA">
  <stTag size="421765">
    <price>965</price>
    <Curr>$</Curr>
  </stTag>
</Country>
<Country name="IN">
  <stTag size="458765">
    <price>895</price>
    <Curr>RS</Curr>
  </stTag>
</Country>
<Country name="TA">
  <stTag size="68765">
    <price>655</price>
    <Curr>RP</Curr>
  </stTag>
</Country>
</price_pt>
<service_code>
  <service_log>N</service_log>
</service_code>
<text_part>
<text sp="IN" art="VSS">WinXP</text>
<text sp="IT" art="SSS">Windows Operating System</text>
</text_part>
</root>

-------------------


Imports System
Imports System.IO
Imports System.Xml

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim xmlDoc As XmlDocument
            Dim xNode As XmlNode
            Try
                xmlDoc = New XmlDocument()
                xmlDoc.Load(OpenFileDialog1.FileName)
                xNode = xmlDoc.SelectSingleNode("//Country[@name='IN']")
                MsgBox(xNode.ChildNodes(0).Item("price").InnerText)
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                If Not xmlDoc Is Nothing Then
                    xmlDoc = Nothing
                End If
            End Try
        End If

    End Sub

End Class


Zulu
0
 
J_MakCommented:
What do you mean by 'i don't want to read all the attributes in that xml file'? Your needed values are selecting certain XML elements, but retaining all of the attributes, you aren't filtering any attributes out. Do you mean reading only some of the elements? Because in that case, you can use a stylesheet which hand picks any element that you want. For examlple:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml"/>
   <xsl:variable name="newline">
      <xsl:text/>
   </xslout:variable>
   <xsl:template match="/">
      <xsl:apply-templates select="pt"/>
   </xsl:template>
   <xsl:template match="pt">
      <xsl:copy-of select="child::*[1]"/>
      <xsl:value-of select="$newline"/>
      <xsl:copy-of select="child::*[3]"/>
      <xsl:value-of select="$newline"/>
      <xsl:copy-of select="child::*[4]"/>
      <xsl:value-of select="$newline"/>
      <xsl:apply-templates select="price_pt"/>
   </xsl:template>
   <xsl:template match="price_pt">
       <xsl:for-each select="Country[@name'IN']">
          <xsl:copy-of select="stTag/price"/>
          <xsl:value-of select="$newline"/>
       </xsl:for-each>
       <xsl:apply-templates select="text_part"/>
   </template>
   <xsl:template match="text_part">
      <xsl:copy-of select="child::*[2]"/>
      <xsl:value-of select="$newline"/>
   </xsl:template>
</xsl:stylesheet>

I haven't tried this out, but it should work. It should also give you an outline of what you need to do.

Hope that helps!
0
 
krishna_tmAuthor Commented:
Hi J_Mak,
    Maybe I shld've looked up atleast the technical jargon of XML to explain myself properly. Anyway, I'd like to read only the specified values, as mentioned in my previous post and that's only a part of xml file i've. I'd like to have a program where i can check the element tag or its value say, if country code is "IN" then read its price otherwise don't. or something like that. Anyway, i ran your XSLT program, it gave me the following error message.

End tag 'xslout:variable' does not match the start tag 'xsl:variable'. Error processing resource 'file:///C:/Test2.xsl'. L...

 </xslout:variable>

Can you make out something from this? Thanx for your input again.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
J_MakCommented:
Try this:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml"/>
   <xsl:variable name="newline">
      <xsl:text/>
   </xsl:variable>
   <xsl:template match="/">
      <xsl:apply-templates select="pt"/>
   </xsl:template>
   <xsl:template match="pt">
      <xsl:copy-of select="child::*[1]"/>
      <xsl:value-of select="$newline"/>
      <xsl:copy-of select="child::*[3]"/>
      <xsl:value-of select="$newline"/>
      <xsl:copy-of select="child::*[4]"/>
      <xsl:value-of select="$newline"/>
      <xsl:apply-templates select="price_pt"/>
   </xsl:template>
   <xsl:template match="price_pt">
       <xsl:for-each select="Country[@name'IN']">
          <xsl:copy-of select="stTag/price"/>
          <xsl:value-of select="$newline"/>
       </xsl:for-each>
       <xsl:apply-templates select="text_part"/>
   </template>
   <xsl:template match="text_part">
      <xsl:copy-of select="child::*[2]"/>
      <xsl:value-of select="$newline"/>
   </xsl:template>
</xsl:stylesheet>

I hope that works now. Cheers!
0
 
krishna_tmAuthor Commented:
Hi J_Mak,
    Thanx for the XSLT code but i'd really prefer if i could keep it in vb.net. I did try your code it was giving tje following error, which i completely don't understand!
"Keyword xsl:stylesheet may not contain PCDATA nodes"

Hi Zulu, the code you gave in vb.net worked fine. But what if my xml file is like following?
How do i retrieve the value of country "IN" for different values, like following?

1. <Country name="IN">
  <stTag size="458765">
    <price>895</price>
2. <Country name="IN">
  <stTag size="46320">
    <price>635</price>
3.<Country name="IN">
  <stTag size="46320">
    <price>635</price>
Like this if i've more than one material and for each material, i've to retrieve the price of those materials only for country "IN"? Now i used your code in my program, it just takes the first value and just repeats it for others, even if i set the
         xnode = nothing
before i go to next record!!! Have any thoughts on this?

XML File:
----------
<?xml version = "1.0"?>
<root>
<start>
<pt>
   <nt ntyp="ekv" le="GHI09"></nt>
   <nt ntyp="esv" le="JLK32"></nt>
   <nt ntyp="ptv" le="1239"></nt>
   <Win typ="98"></Win>
</pt>
<price_pt>
<Country name="DA">
  <stTag size="421765">
    <price>965</price>
    <Curr>$</Curr>
  </stTag>
</Country>
<Country name="IN">
  <stTag size="458765">
    <price>895</price>
    <Curr>RS</Curr>
  </stTag>
</Country>
<Country name="TA">
  <stTag size="68765">
    <price>655</price>
    <Curr>RP</Curr>
  </stTag>
</Country>
</price_pt>
<service_code>
  <service_log>N</service_log>
</service_code>
<text_part>
<text sp="IN" art="VSS">WinXP</text>
<text sp="IT" art="SSS">Windows Operating System</text>
</text_part>
</start>
<start>
<pt>
   <nt ntyp="ekv" le="GHI09"></nt>
   <nt ntyp="esv" le="JLK32"></nt>
   <nt ntyp="ptv" le="1239"></nt>
   <Win typ="98"></Win>
</pt>
<price_pt>
<Country name="DA">
  <stTag size="421765">
    <price>965</price>
    <Curr>$</Curr>
  </stTag>
</Country>
<Country name="IN">
  <stTag size="23765">
    <price>785</price>
    <Curr>RS</Curr>
  </stTag>
</Country>
<Country name="TA">
  <stTag size="68765">
    <price>655</price>
    <Curr>RP</Curr>
  </stTag>
</Country>
</price_pt>
<service_code>
  <service_log>N</service_log>
</service_code>
<text_part>
<text sp="IN" art="VSS">WinXP</text>
<text sp="IT" art="SSS">Windows Operating System</text>
</text_part>
</start>
<start>
<pt>
   <nt ntyp="ekv" le="GHI09"></nt>
   <nt ntyp="esv" le="JLK32"></nt>
   <nt ntyp="ptv" le="1239"></nt>
   <Win typ="98"></Win>
</pt>
<price_pt>
<Country name="DA">
  <stTag size="98325">
    <price>1005</price>
    <Curr>$</Curr>
  </stTag>
</Country>
<Country name="IN">
  <stTag size="46320">
    <price>635</price>
    <Curr>RS</Curr>
  </stTag>
</Country>
<Country name="TA">
  <stTag size="68765">
    <price>655</price>
    <Curr>RP</Curr>
  </stTag>
</Country>
</price_pt>
<service_code>
  <service_log>N</service_log>
</service_code>
<text_part>
<text sp="IN" art="VSS">WinXP</text>
<text sp="IT" art="SSS">Windows Operating System</text>
</text_part>
</start>
</root>

0
 
krishna_tmAuthor Commented:
Dear Admin,
      I'd like to know how to allocate the points for Zulu. Though its not the accepted answer, it did give me some idea of doing it. Can I give points like, say 200 to Zulu and retain my 100 points, is that possible. If its not, then its ok to give all points to zulu. Pls let me know abt this. Thanx a lot,

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.