[RESOLVED] Dataset readxml - read only certain elements(columns)
Hi guys :wave:
Dataset's ReadXML() method would read an XML document. But how would we skip certain columns ?
For example:
Code:
<product>
<book>
<id>1</id>
<name>abc1</name>
<price>Rs. 100</price>
</book>
<book>
<id>2</id>
<name>abc2</name>
<price>Rs. 250</price>
</book>
<book>
<id>3</id>
<name>aaasd</name>
<price>Rs. 12345</price>
</book>
</product>
So, if I use ReadXML(), it would read the whole document and the dataTable would have "id" column, "name" column and "price" column.
But I want to have only "id" and "name" column. How can I skip certain columns from reading it ?
Thanks :wave:
Re: Dataset readxml - read only certain elements(columns)
Next time I should spend a few minutes experimenting before posting the question in here. Because I found the solution after spending 15 minutes of experiments. :p
Solution:
vb.net Code:
'~~~ ds is the DataSet that reads the XML document. It's 0th indexed DataTable is the actual table that contains the data. So, we could call the RemoveAt() or Remove() method to remove the unwanted columns...!
ds.Tables(0).Columns.RemoveAt(3) '~~~ index of column to be removed is 3
:wave:
Re: [RESOLVED] Dataset readxml - read only certain elements(columns)
This is not fair Akki. :( Provide an opportunity for someone else to resolve the thread. :D
Re: [RESOLVED] Dataset readxml - read only certain elements(columns)
Quote:
Originally Posted by
Ram2Curious
This is not fair Akki. :( Provide an opportunity for someone else to resolve the thread. :D
:lol:
Also found that, there's no need of using a DataSet. Only DataTable is needed as it has both the ReadXML() and WriteXML() methods. Previously, I was thinking that, those methods were available only in a DataSet. :)
Another thing that I found is, when using WriteXML() method, we have to use a 2nd parameter called "XmlWriteMode.WriteSchema" to write the schema in the XML file. Otherwise, when we try to read this XML file(which we have created), VB would pop an error message saying it couldn't harness the schema.
:wave: