Hi guys:
I am a xml file like follow:
<?xmlversion="1.0"encoding="utf-8"?><Response><Roomname="ro1"><Chairname="ch1">ABCD1</Chair></Room><Roomname="ro2"><Chairname="ch2">ABCD2</Chair></Room><Roomname="ro3"><Chairname="ch3">ABCD3</Chair></Room><Roomname="ro4"><Chairname="ch4">ABCD4</Chair></Room></Response>
and I read it:
while (xmlReader.Read()) {switch (xmlReader.Name) {case"Room":if(xmlReader.IsStartElement()) { Console.WriteLine("start room"); }else { Console.WriteLine("end room"); }break;case"Chair":if (xmlReader.IsStartElement()) { Console.WriteLine("start chair"); }else { Console.WriteLine("end chair"); }break; } }
it works fine, and print out
start room
start chair
end chair
end room
start room
start chair
end chair
end room
start room
start chair
end chair
end room
start room
start chair
end chair
end room
that's what I excepted!
but if I change the code like:
while (xmlReader.Read()) {switch (xmlReader.Name) {case"Room":if(xmlReader.IsStartElement()) { Console.WriteLine("start room"); }else { Console.WriteLine("end room"); }break;case"Chair":if (xmlReader.IsStartElement()) { Console.WriteLine("start chair"); }else { Console.WriteLine("end chair"); }var chairContent = xmlReader.ReadElementContentAsString(); Console.WriteLine("chair content is:"+chairContent);break; } }
then the end tag of chair can not be reached! out put like:
start room
start chair
chair content is:ABCD1
end room
start room
start chair
chair content is:ABCD2
start room
start chair
chair content is:ABCD3
end room
start room
start chair
chair content is:ABCD4
end room
if I change the xml, delete the new line between chair and room, like
<?xmlversion="1.0"encoding="utf-8"?><Response><Roomname="ro1"><Chairname="ch1">ABCD1</Chair></Room><Roomname="ro2"><Chairname="ch2">ABCD2</Chair></Room><Roomname="ro3"><Chairname="ch3">ABCD3</Chair></Room><Roomname="ro4"><Chairname="ch4">ABCD4</Chair></Room></Response>
both the end chair and end room can not be reached, print like follow:
start room
start chair
chair content is:ABCD1
start room
start chair
chair content is:ABCD2
start room
start chair
chair content is:ABCD3
start room
start chair
chair content is:ABCD4
zbo