Can an XML file be converted into a valid DataSource?

S

Sky

What makes something a valid DataSource? What methods/iterators/etc?

Why do I ask?
I do understand that a DataSet is based on an XML structure...but it's too
table structured for what I am thinking...
Can one read in a an xml file that has various embedded nodes (ie: records
that have children records as XML does best) -- possibly not all the same
length (ie, dif 'column count' for certain 'rows') and try to shove that
into a DataRepeater?

In other words, if I were trying to make a TreeList (Tree with several
columns) based on a DataRepeater, could I read from an XML file? Or do I
have to convert the XML file node by node into a DataSet first?

PS: If anybody has links to someone who has tried this approach, or
similar -- love to hear where!

Best,
Sky
 
S

Sky

You're a godsend Saravana! The following is just what I needed!
Understanding that the doc is not IEnumerable -- but a returned collection
is -- is just the missing piece I was looking for!

Dim oXML As New XmlDataDocument
oXML.Load(Server.MapPath("product.xml"))
DataGrid1.DataSource = oXML.SelectNodes("DataSet/Product")
DataGrid1.DataBind()

This leads me try to see into the future : if I want to take this further
and make a DataRepeater based TreeList from this (see moanings looking for a
good TreeList (Tree w/multiple columns) in messages further down...) I would
have to do some kind of recursive DataRepeater that binds that may look
something like:

void RenderNode(oNode){
oSet = oNode.SelectNodes("child::*");
...hum...somewhere around here I am lost...
...but it would somehow look like attaching to a datarepeater that
ejects a div with spans in it...

...but the next part to reiterate would be
foreach (oSubNode in oSet){
RenderNode(oSubNode);
}

}

//How's that for totally unclear, lost, proxy code!!! (I'm having such
trouble getting a grip on ASP.NET some days, it really isn't funny!)
//Any ideas on how to make this make sense in any way???
 
S

Sky

"One last question":
What happens when you bind an IEnumerable datasource to a control...but it's
missing parts of it?

What I mean is what would happen if you bind as the article suggests but the
xml datasource looks like (missing some fields from some of the records):

<?xml version="1.0" standalone="yes"?>
<DataSet>
<Product>
<Name>Onida TV</Name>
<ProductID>1</ProductID>
<Price>12000</Price>
<Quantity>2</Quantity>
</Product>
<Product>
<Name>Samsung TV</Name>
<ProductID>2</ProductID>
</Product>
<Product>
<Name>LG TV</Name>
<ProductID>3</ProductID>
</Product>
</DataSet>

According to me, this would cause a big fat exception. How would you trap,
replace with a 0, and move on to next field? Since about the last access to
the code is at

try {
Grid=oNode.selectNodes("child::*")
}
catch {}

it would just abort/rollback all the binding... how to trap down at a more
granular method, at the cell(field)-binding level?

Any suggestions?
Or definatly not possible?

Thanks,
Sky
 
S

Sky

Dear Michael:
In regards to my question "what qualifies something as iEnumerable and
therefore a valid datasource", turns out that when I did a search of my code
I must have already investigated (and promptly forgot!) how to make an
IEnumerable object... So, with your reminder, and the piece of code that i
found, case [almost] closed, thanks!!!

My only remaining question is making it IEnumerable, does this really do ALL
that is required to qualify it as a Valid datasource for eg:DataGrid,
etc. -- and what happens when a 'piece' (a field) is missing from the source
(what I mean by this is if you define an xml recordset as a DataSource to eg
a DataGrid -- but some of the records are incomplete and are missing
sub-node/fields in some cases). What happens? Exception? If so, any way that
it can be trapped/corrected, and move on to next fields and rows?

Sky




PS:
Code found was (for anybody who can use it when reading these posts)
Turns out that the "Recipe" is really simple: you only have to enherit from
IEnumerable, and overload the base.GetEnumerator() and MoveNext() and
Current property . No sweat:
public class cSchemaInfoColumnCollection : System.Collections.IEnumerable

{

private System.Collections.Hashtable _List = new
System.Collections.Hashtable();

public cSchemaInfoColumn this[string qName]

{

get

{

return (cSchemaInfoColumn)_List[qName.ToLower()];

}

set

{

_List[qName.ToLower()] = value;

}

}

/*----------*/

public cMyEnumerator GetEnumerator()

{

return new cMyEnumerator(_List);

}

/*----------*/

// Implement the GetEnumerator() method:

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()

{

return GetEnumerator();

}

/*----------*/

/*----------*/

/*----------*/

// Declare the enumerator and implement the IEnumerator interface:

public class cMyEnumerator: System.Collections.IEnumerator

{

int nIndex;

System.Collections.Hashtable _List;

string[] tKeys;

/*----------*/

public cMyEnumerator(System.Collections.Hashtable qList)

{

_List = qList;

tKeys = new string[_List.Count];

_List.Keys.CopyTo(tKeys,0);

;

nIndex = -1;

}

/*----------*/

public void Reset()

{

nIndex = -1;

}

/*----------*/

public bool MoveNext()

{

nIndex++;

return(nIndex < tKeys.Length);

}

/*----------*/

// The current property on the IEnumerator interface:

object System.Collections.IEnumerator.Current

{

get

{

return(Current);

}

}

/*----------*/

public cSchemaInfoColumn Current

{

get

{

return((cSchemaInfoColumn)_List[tKeys[nIndex]]);

}

}

/*----------*/

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top