?
Solved

loading Selected nodes of XML in a dataset

Posted on 2006-07-18
18
Medium Priority
?
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
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 500 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

800 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