Xml Woe

Ok, so I have a function to parse an xml file in my application. Now, the only problem is, I need to know what level I'm at, and I move up a level when I find a Node of type Element, and go down a level when I find ElementEnd. My problem is, when I come across an element where it looks like this:

<element />

Instead of:

<element> </element>

It messes me up, because it's only picked up as an element, and no element end.

Here's the code I'm using:

langReader is System.Xml.XmlReader
fParseLog is just a form with a log on it

while ( langReader.Read( ) )
{
// This variable stores what each line in the log window is
sWindowString = "";

// Use a switch flow-control to determine the type of node that we are currently dealing with
switch ( langReader.NodeType )
{
case XmlNodeType.Element:
// Switch the current element
iCurLevel++;
sCurElement[iCurLevel] = langReader.Name;
fParseLog.logEvent( "\r\n" + CreateTabs( iCurLevel ) + "<" + langReader.Name + "> " );
break;

case XmlNodeType.EndElement:
// Just for debug purposes.
fParseLog.logEvent( "\r\n" + CreateTabs( iCurLevel ) + "</" + langReader.Name + ">" );
if ( langReader.Name == sCurElement[ iCurLevel ] )
{
// We've cloesed an element; Close the highest level element
iCurLevel--;
}
break;

case XmlNodeType.Text:
// We have a node other then an element, so switch around
// to see what we are dealing with
fParseLog.logEvent( " " + langReader.Value );

switch ( sCurElement[ iCurLevel ] )
{
case "comment":
// Add the text in between the element tags to
// the comments of the temp language
langTemp.addComment( langReader.Value );
break;

case "function":
// We have a function. Add the text in between
// the element tags.
langTemp.addFunction( langReader.Value );
break;
}
break;
}

if ( langReader.HasAttributes )
{
while ( langReader.MoveToNextAttribute( ) )
{
switch ( sCurElement[ iCurLevel ] )
{
case "language":
if ( langReader.Name == "title" )
{
// It's the title
sLangTitle = langReader.Value;
fParseLog.changeTitle( langReader.Value );
langTemp.setTitle( sLangTitle );
}
break;
}
fParseLog.logEvent( "Attribute: " + langReader.Name + " = " + langReader.Value );
}
// Move the reader back to the element node.
langReader.MoveToElement( );
}

// Log event
//fParseLog.logEvent( langReader.Value.ToString() );
}

Any help would be great :)

[2807 byte] By [Joudoki] at [2007-12-22]
# 1

Alas.... I think what you'll have to do is put in a test within your first case to see if it is empty i.e. <element />. Something along the lines of.....

case XmlNodeType.Element:
// Switch the current element

iCurLevel++;
sCurElement[iCurLevel] = langReader.Name;
fParseLog.logEvent( "\r\n" + CreateTabs( iCurLevel ) + "<" + langReader.Name + "> " );

// Check for this being an empty element - this assumes that you have not done a Read or ReadElementContentAsXXXX operation on the
// reader so far in this case
if (langReader.IsEmptyElement)
{
// Do Stuff that you would normally do for an end element
...
}
break;
....

mogwai at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...

.NET Development

Site Classified