Link to home
Start Free TrialLog in
Avatar of krishna_tm
krishna_tm

asked on

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>
Avatar of J_Mak
J_Mak

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!
Avatar of krishna_tm

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of zulu_11
zulu_11

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
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>

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,