all groups > dotnet xml > august 2005 >
You're in the

dotnet xml

group:

ReadOuterXml Locks Up


ReadOuterXml Locks Up TJO
8/29/2005 5:12:10 PM
dotnet xml:
The following code snippet seems to successfully read the first
outerxml but on the second time in the loop it locks up. Can anyone
see why?

Thanks

<code snippet>

using (FileStream fs = File.Open(test._LogFileName,
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
reader = new XmlTextReader(fs);

while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.Equals("MITest"))
{
stringBuilder.Append(reader.ReadOuterXml()); // locks up here
when reading the second MITest element
}
}
}
}
</code snippet>


<XMLFileBeingRead>

<MITest Test="Nothing Test" Object="Foo">
<StartTime>8/29/2005 4:50:12 PM</StartTime>
<EndTime>8/29/2005 4:50:13 PM</EndTime>
<Result>TimeOut</Result>
</MITest>
<MITest Test="Nothing Test" Object="Foo">
<StartTime>8/29/2005 4:50:29 PM</StartTime>
<EndTime>8/29/2005 4:50:30 PM</EndTime>
<Result>TimeOut</Result>
</MITest>

</XMLFileBeingRead>
Re: ReadOuterXml Locks Up TJO
8/29/2005 7:11:54 PM
Actually it is locking up on the firs call to ReadOuterXml when when
two or mowe MITest elements exists in the xml file.
Re: ReadOuterXml Locks Up TJO
8/29/2005 7:25:51 PM
I was using the SystemInternal FileMon program to watch the XML file as
my program ran and noticed a buffer overflow entry:

7:22:10 PM TestClassTester:1424 QUERY
INFORMATION C:\STProjects\Projects\SmartMI\TestClassTester\bin\Debug\NothingTestLog_Foo.xml BUFFER
OVERFLOW FileFsVolumeInformation
Re: ReadOuterXml Locks Up TJO
8/30/2005 7:06:43 AM
Thanks Oleg,
I suspected it was something like this but my sample still fails after
modifying my code to look like yours.

The failure still fails on the second time the reader.ReadOutXml() line
is reached in the loop. The strange thing is that it is not really a
failure as in the program crashes. It just hangs there.

So I remain stuck :(
Re: ReadOuterXml Locks Up Oleg Tkachenko [MVP]
8/30/2005 10:29:19 AM
[quoted text, click to view]

Well, it works for me, but I see the problem here. Beware that after
reader.ReadOuterXml() call the reader is positioned at the next node
*after* </MITest>. If it's whitespace - it works, if that's next
<MITest> start tag - you loses it by calling reader.Read() in while
statement. In such cases the loop better be implemented using
!reader.EOF as invariant:
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element &&
reader.Name.Equals("MITest"))
{
Console.WriteLine(reader.ReadOuterXml());
}
else
{
reader.Read();
}
}

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
AddThis Social Bookmark Button