Solved

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

Posted on 2004-09-22
9
373 Views
Last Modified: 2008-03-06
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>
0
Comment
Question by:krishna_tm
  • 3
  • 2
9 Comments
 
LVL 7

Expert Comment

by:J_Mak
ID: 12131092
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
 

Author Comment

by:krishna_tm
ID: 12131252
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
 
LVL 6

Accepted Solution

by:
zulu_11 earned 300 total points
ID: 12132182
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:J_Mak
ID: 12132870
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
 

Author Comment

by:krishna_tm
ID: 12140955
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
 

Author Comment

by:krishna_tm
ID: 12186426
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Limiting string to two deciamls 18 49
Round up to 100% in .NET 10 80
Copy/Clone an object. 9 29
Exit the loop 4 31
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now