Solved

loading Selected nodes of XML in a dataset

Posted on 2006-07-18
18
370 Views
Last Modified: 2012-08-14
I have an XML file like this

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <cart>
    <userID>bob</userID>
    <ProductID>2</ProductID>
    <ProductName>Book</ProductName>
    <UnitPrice>185.00</UnitPrice>
    <Quantity>1</Quantity>
  </cart>
  <cart>
    <userID>bob</userID>
    <ProductID>8</ProductID>
    <ProductName>Pen</ProductName>
    <UnitPrice>300.00</UnitPrice>
    <Quantity>1</Quantity>
  </cart>
  <cart>
    <userID>tim</userID>
    <ProductID>8</ProductID>
    <ProductName>Pen</ProductName>
    <UnitPrice>300.00</UnitPrice>
    <Quantity>1</Quantity>
  </cart>
</NewDataSet>


I want to retrieve only those cart elements from xml file matching the userID=bob  and populate Dataset with this Particular information.
In this case I want dataset to have only two datarows only.

Is there anyway I can acheive this.
Thanks.
0
Comment
Question by:skvikram
  • 10
  • 8
18 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17139960
Load the DataSet with DataSet.ReadXML, and then filter by userID = 'bob'.

Bob
0
 
LVL 7

Author Comment

by:skvikram
ID: 17140558
that is possible. what I want is only selected nodes in DATASET not the entire XML file.

I want to populate by using something like this type of statement
select * from [table1] where [condition1]
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17140607
If that is true, then you will have use something like an XmlTextReader or an XmlDocument with XPath expression go get the data and append rows to a DataTable.

Bob
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Author Comment

by:skvikram
ID: 17143541
yes TheLearnedone, I know xpathnavigator and xpathnodeiterator will do,
the job the question is HOW??
Can u supply me some code specific to my problem where UserID is a GUID value in XML file
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17151030
If you want code, do you want VB.NET or C#?  This is the generic .NET topic area, so I can't tell. BTW, there are more specific VB.NET and C# areas that would be more appropriate for this question, since you need code.

Bob
0
 
LVL 7

Author Comment

by:skvikram
ID: 17151887
VB.NET or C# anything will do. I am not language dependent.
since it is generic .NET topic area that's why I asked it here to get maximum responses in any .NET language. but still I am getting response from you only.
0
 
LVL 7

Author Comment

by:skvikram
ID: 17151893
please keep in mind UserID is a GUID value in XML file
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17160384
Do you have .NET 2005, 2003 or 2002?

Bob
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 17160449
Try this:

Imports System.Xml

...

    BuildDataTableFromXml("c:\temp\test.", "userID", "bob")
 
...

  Public Function BuildDataTableFromXml(ByVal rootPath As String, ByVal filterColumn As String, ByVal filterCondition As String) As DataTable

    Dim xsdFileName As String = rootPath & "xsd"
    Dim xmlFileName As String = rootPath & "xml"

    Dim table As DataTable

    Try

      ' Build an XmlDataDocument that is optimized for DataSet operations
      Dim doc As New XmlDataDocument
      doc.DataSet.ReadXmlSchema(xsdFileName)
      doc.Load(xmlFileName)

      ' Clone the structure of the table to add certain records to.
      table = doc.DataSet.Tables(0).Clone

      ' Go through all the record nodes, looking for any condition, and
      ' adding them to the returned DataTable.
      For Each node As XmlElement In doc.SelectNodes("//cart")

        ' Build a DataRow from the 'cart' element.
        Dim row As DataRow = doc.GetRowFromElement(node)

        ' Add a row to DataTable, if the filter condition is not valid or
        ' if the filter condition is met.
        Dim willAddRow As Boolean = (filterColumn.Length = 0 Or filterCondition.Length = 0) OrElse row(filterColumn).ToString() = filterCondition

        If willAddRow Then
          table.ImportRow(row)
        End If
      Next

    Catch ex As Exception
      MessageBox.Show(ex.ToString)
    End Try

    Return table

  End Function

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17160460
C#:

using System;
using System.Data;
using System.Xml;

public class DataSetBuilder
{

  public static DataTable BuildDataTableFromXml(string rootPath, string filterColumn, string filterCondition)
  {
    string xsdFileName = rootPath + "xsd";
    string xmlFileName = rootPath + "xml";
    DataTable table;
    XmlDataDocument doc = new XmlDataDocument();
    doc.DataSet.ReadXmlSchema(xsdFileName);
    doc.Load(xmlFileName);
    table = doc.DataSet.Tables[0].Clone();
    foreach (XmlElement node in doc.SelectNodes("//cart"))
    {
      DataRow row = doc.GetRowFromElement(node);
      bool willAddRow = (filterColumn.Length == 0 | filterCondition.Length == 0) || row[filterColumn].ToString() == filterCondition;
      if (willAddRow)
      {
        table.ImportRow(row);
      }
    }
    return table;
  }
}

Bob
0
 
LVL 7

Author Comment

by:skvikram
ID: 17160963
I wanted to load the xml file in a dataset which has other datatables also.
I don't know how to convert the datatable obtained from your code into a dataset. And utilize it in my existing code.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17161702
Public Function BuildDataFromXml(ByVal rootPath As String, ByVal filterColumn As String, ByVal filterCondition As String) As DataSet

Dim ds As New DataSet

...
' Do the other code here

ds.Tables.Add(table)

Return ds

Bob
0
 
LVL 7

Author Comment

by:skvikram
ID: 17175576
thanks,
But How can I add new entries in XML file when I get the contents of XML file in a dataset through datatable.
Is it possible
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17175706
If you load the DataSet with DataSet.ReadXml, it would be trivial.  But, if you are handling the XML parsing, it would be much more difficult.

Bob
0
 
LVL 7

Author Comment

by:skvikram
ID: 17248340
>>>>If you load the DataSet with DataSet.ReadXml, it would be trivial.  But, if you are handling the XML parsing, it would be much more difficult.<<<<

So how to accomplish the job of adding new entries in XML file when I get the contents of XML file in a dataset through datatable.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17254178
Are you asking how to add rows to a DataSet that is loaded my way or the DataSet.ReadXML way?

If you are talking about my code, then table.Rows.Add(row) is all you need.

Bob
0
 
LVL 7

Author Comment

by:skvikram
ID: 17256907
i am asking about ur way of rows addition.

can u please explain by a little example.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17258946
Since you get a DataTable from BuildDataTableFromXml, this is how you would add a row:

DataRow row = table.NewRow();
row["Field"] = "Value";
table.Rows.Add(row);

Bob
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

831 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