Thanks for replying Derek. The only time I call the XmlTextReader.Read()
method is during the while loop test. Inside the while loop, however,
ReadOuterXml() is called multiple times, but with awareness of what it's
doing.
The stripped down Parse function looks like this:
public override void Parse(string nodeText)
{
XmlTextReader xr = new XmlTextReader(new StringReader(nodeText));
while(xr.Read())
{
switch(xr.Name)
{
case ND_PERSON:
{
if (xr.Depth == 0)
{
string sex = xr.GetAttribute(AT_SEX);
if (sex != null)
Sex = sex;
string previousName = xr.GetAttribute(AT_PREVIOUS_NAME);
if (previousName != null)
PreviousName = previousName;
string firstHomeBuyer =
xr.GetAttribute(AT_FIRST_HOME_BUYER);
if (firstHomeBuyer != null)
FirstHomeBuyer =
LixiParser.ConvertToBoolean(firstHomeBuyer);
string yearsIn = xr.GetAttribute(AT_YEARS_IN_PROFESSION);
if (yearsIn != null)
YearsInCurrentProfession = int.Parse(yearsIn);
}
break;
}
case ND_RELATED_ENTITY_REF:
{
if (xr.Depth == 1)
{
string outerXml = xr.ReadOuterXml();
RelatedEntityRef.Parse(outerXml);
}
break;
}
case ND_PERSON_NAME:
{
if (xr.Depth == 1)
{
string outerXml = xr.ReadOuterXml();
PersonName.Parse(outerXml);
}
break;
}
}
}
xr.Close();
}
There's heaps more in it, but they're all the same as the ND_PERSON_NAME
case, just with different constants and different classes parsing the outer
XML. I'm aware that ReadOuterXml() advances the reader, but it should only
advance it to the next node after the current node's closing tag, surely?
The whole point in the design is each class knows how to parse itself from
an xml fragment passed into it. Each class is aware of the children of its
element, and so it hands off responsibility for parsing the child element to
an agregated child class.
The root class's parsing code starts like this:
public override void Parse(string lixiText)
{
XmlTextReader xr = null;
XmlDocument doc = new XmlDocument();
try
{
doc.LoadXml(lixiText);
xr = new XmlTextReader(new StringReader(doc.OuterXml));
}
catch
{
xr = new XmlTextReader(new StringReader(lixiText));
}
while(xr.Read())
I've just added the try/catch with the XmlDocument class trying to see if it
would have a problem parsing the text - it doesn't. That code won't be
staying there for obvious reasons.
Does this give you anymore information?
"Derek Harmon" wrote:
"Geoff Bennett" <Ge**********@discussions.microsoft.com> wrote in message news:43**********************************@microsof t.com... While parsing an XML document, my TextReader instance skips nodes.
: : <Person Sex="Male" FirstHomeBuyer="No" YearsInCurrentProfession="14">
<RelatedEntityRef RelatedID="118"/>
<PersonName>
<NameTitle Value="Mr"/>
: : The parsing code will hit the "Person" element and the "RelatedEntityRef"
element, but it skips "PersonName" and winds up on "NameTitle".
: : Can anyone give me some indication of what I'm overlooking here?
The most common cause of this symptom is that XmlTextReader's
Read( ) method is being called but the processing logic is skipping
handling the node. Often, pretty-printing so that you have insignifi-
cant whitespace nodes will hide this flaw. Most XmlTextReader
logic is performed within a loop of some sort. Lookout for Read( )
appearing inside of the loop (definite warning sign), you want to
ensure Read( ) is happening only once per loop.
What code are you using the read from the XmlTextReader?
There's almost certainly some flow of control that's skipping over
the PersonName element.
Derek Harmon