Log in

View Full Version : reading of XML tag(using smartdevice)


mindset
09-28-2004, 07:54 AM
hi.. i have problem on reading XML string data..

i need to receive this stream of XML tag..

<show>

<Title date="8-12-04" time="6 pm" place="lot 1">The Great One</Title>

<Title date="8-12-04" time="7 pm" place="lot 2">The Great Two</Title>

<Title date="8-12-04" time="8 pm" place="lot 3">The Great Three</Title>
.
.
.

</show>

how should go about retriving the data(places and show name) like, if i want to watch any show on 8-12-04 , and i am able to see the place and the movie names?




thanks....

GSmith
10-05-2004, 03:49 PM
The trick is that you have both elements with text and attributes. As you parse, you get the attributes as a specific node. The NEXT node after the Element is the Text. This is tricky to code. This code includes a MessageBox that displays on every node.

There may be better ways that more fully involve the capabilities of the Xml parsing in Compact Framework. But this will read the file you requested:


public ArrayList ReadXml ( string filename )
{
/* This method reads the following Xml text file
<show>
<Title date="8-12-04" time="6 pm" place="lot 1">The Great One</Title>
<Title date="8-12-04" time="7 pm" place="lot 2">The Great Two</Title>
<Title date="8-12-04" time="8 pm" place="lot 3">The Great Three</Title>
</show>
*/
ArrayList performances = new ArrayList() ;

Stack elementStack = new Stack() ;

System.IO.Stream stream = System.IO.File.Open(filename,System.IO.FileMode.Open) ;
XmlTextReader xtr = new XmlTextReader(stream);
xtr.WhitespaceHandling = System.Xml.WhitespaceHandling.Significant ;
Hashtable attributes = null ;
while ( xtr.Read() )
{
if ( xtr.NodeType.Equals (System.Xml.XmlNodeType.Element ) )
elementStack.Push(xtr.Name) ;
// TODO: Add error detection on Xml Element Pop.
if ( xtr.NodeType.Equals (System.Xml.XmlNodeType.EndElement ) )
elementStack.Pop() ;
if ( xtr.Value.Equals("") && ! xtr.HasAttributes ) // ignore blank elements without attributes
continue ;

MessageBox.Show("("+xtr.NodeType.ToString()+")"+xtr.Name.ToString()+": "+xtr.Value.ToString()) ;

if ( xtr.NodeType.Equals(System.Xml.XmlNodeType.Text) && (null != attributes) )
{
attributes.Add(elementStack.Peek(),xtr.Value); // Add Title as attribute
performances.Add(attributes) ;
attributes = null ;
}
else if ( xtr.Name.Equals("Title" ) )
{

attributes = new Hashtable() ;

if ( xtr.HasAttributes )
{
xtr.MoveToFirstAttribute() ;
do
{
attributes.Add(xtr.Name,xtr.Value);
} while ( xtr.MoveToNextAttribute() ) ;
}
}
}
// performances now has the data
return performances ;
}