Xmlreader read end tag problem

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi guys:
I am a xml file like follow:

<div style="color:black; background-color:white
<pre><span style="color:blue <?<span style="color:#a31515 xml <span style="color:red version<span style="color:blue =<span style="color:black "<span style="color:blue 1.0<span style="color:black " <span style="color:red encoding<span style="color:blue =<span style="color:black "<span style="color:blue utf-8<span style="color:black "<span style="color:blue ?>
<span style="color:blue <<span style="color:#a31515 Response<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Room <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ro1<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Chair <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ch1<span style="color:black "<span style="color:blue >ABCD1<span style="color:blue </<span style="color:#a31515 Chair<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Room<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Room <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ro2<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Chair <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ch2<span style="color:black "<span style="color:blue >ABCD2<span style="color:blue </<span style="color:#a31515 Chair<span style="color:blue ><span style="color:blue </<span style="color:#a31515 Room<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Room <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ro3<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Chair <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ch3<span style="color:black "<span style="color:blue >ABCD3<span style="color:blue </<span style="color:#a31515 Chair<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Room<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Room <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ro4<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Chair <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ch4<span style="color:black "<span style="color:blue >ABCD4<span style="color:blue </<span style="color:#a31515 Chair<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Room<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Response<span style="color:blue >
[/code]


and I read it:

<div style="color:black; background-color:white
<pre><span style="color:blue while (xmlReader.Read())
{
<span style="color:blue switch (xmlReader.Name)
{
<span style="color:blue case <span style="color:#a31515 "Room":
<span style="color:blue if(xmlReader.IsStartElement())
{
Console.WriteLine(<span style="color:#a31515 "start room");
}
<span style="color:blue else
{
Console.WriteLine(<span style="color:#a31515 "end room");
}
<span style="color:blue break;
<span style="color:blue case <span style="color:#a31515 "Chair":
<span style="color:blue if (xmlReader.IsStartElement())
{
Console.WriteLine(<span style="color:#a31515 "start chair");
}
<span style="color:blue else
{
Console.WriteLine(<span style="color:#a31515 "end chair");
}
<span style="color:blue break;
}
}
[/code]


it works fine, and print out
start room<br/>
start chair<br/>
end chair<br/>
end room<br/>
start room<br/>
start chair<br/>
end chair<br/>
end room<br/>
start room<br/>
start chair<br/>
end chair<br/>
end room<br/>
start room<br/>
start chair<br/>
end chair<br/>
end room


thats what I excepted!
but if I change the code like:

<div style="color:black; background-color:white
<pre><span style="color:blue while (xmlReader.Read())
{
<span style="color:blue switch (xmlReader.Name)
{
<span style="color:blue case <span style="color:#a31515 "Room":
<span style="color:blue if(xmlReader.IsStartElement())
{
Console.WriteLine(<span style="color:#a31515 "start room");
}
<span style="color:blue else
{
Console.WriteLine(<span style="color:#a31515 "end room");
}
<span style="color:blue break;
<span style="color:blue case <span style="color:#a31515 "Chair":
<span style="color:blue if (xmlReader.IsStartElement())
{
Console.WriteLine(<span style="color:#a31515 "start chair");
}
<span style="color:blue else
{
Console.WriteLine(<span style="color:#a31515 "end chair");
}
<span style="color:blue var chairContent = xmlReader.ReadElementContentAsString();
Console.WriteLine(<span style="color:#a31515 "chair content is:"+chairContent);
<span style="color:blue break;
}
}
[/code]


then the end tag of chair can not be reached! out put like:
start room<br/>
start chair<br/>
chair content is:ABCD1<br/>
end room<br/>
start room<br/>
start chair<br/>
chair content is:ABCD2<br/>
start room<br/>
start chair<br/>
chair content is:ABCD3<br/>
end room<br/>
start room<br/>
start chair<br/>
chair content is:ABCD4<br/>
end room
if I change the xml, delete the new line between chair and room, like

<div style="color:black; background-color:white
<pre><span style="color:blue <?<span style="color:#a31515 xml <span style="color:red version<span style="color:blue =<span style="color:black "<span style="color:blue 1.0<span style="color:black " <span style="color:red encoding<span style="color:blue =<span style="color:black "<span style="color:blue utf-8<span style="color:black "<span style="color:blue ?>
<span style="color:blue <<span style="color:#a31515 Response<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Room <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ro1<span style="color:black "<span style="color:blue ><span style="color:blue <<span style="color:#a31515 Chair <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ch1<span style="color:black "<span style="color:blue >ABCD1<span style="color:blue </<span style="color:#a31515 Chair<span style="color:blue ><span style="color:blue </<span style="color:#a31515 Room<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Room <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ro2<span style="color:black "<span style="color:blue ><span style="color:blue <<span style="color:#a31515 Chair <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ch2<span style="color:black "<span style="color:blue >ABCD2<span style="color:blue </<span style="color:#a31515 Chair<span style="color:blue ><span style="color:blue </<span style="color:#a31515 Room<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Room <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ro3<span style="color:black "<span style="color:blue ><span style="color:blue <<span style="color:#a31515 Chair <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ch3<span style="color:black "<span style="color:blue >ABCD3<span style="color:blue </<span style="color:#a31515 Chair<span style="color:blue ><span style="color:blue </<span style="color:#a31515 Room<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Room <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ro4<span style="color:black "<span style="color:blue ><span style="color:blue <<span style="color:#a31515 Chair <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue ch4<span style="color:black "<span style="color:blue >ABCD4<span style="color:blue </<span style="color:#a31515 Chair<span style="color:blue ><span style="color:blue </<span style="color:#a31515 Room<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Response<span style="color:blue >
[/code]


both the end chair and end room can not be reached, print like follow:
start room<br/>
start chair<br/>
chair content is:ABCD1<br/>
start room<br/>
start chair<br/>
chair content is:ABCD2<br/>
start room<br/>
start chair<br/>
chair content is:ABCD3<br/>
start room<br/>
start chair<br/>
chair content is:ABCD4
<
zbo<br/>

View the full article
 
Back
Top