Czytam plik xml o nieznanej długości i czytając każdy element w strukturze listy. Teraz, gdy dojdę do końca pliku, kontynuuję czytanie, to powoduje wyjątek. Właśnie złapałem ten wyjątek i kontynuowałem swoje życie, ale czy istnieje lepszy sposób na zrobienie tego?Jak obsługiwać koniec pliku podczas odczytu pliku XML
try
{
while(!textReader.EOF)
{
// Used to store info from each command as they are read from the xml file
ATAPassThroughCommands command = new ATAPassThroughCommands();
// the following is just commands being read and their contents being saved
XmlNodeType node = textReader.NodeType;
textReader.ReadStartElement("Command");
node = textReader.NodeType;
name = textReader.ReadElementString("Name");
node = textReader.NodeType;
CommandListContext.Add(name);
command.m_Name = name;
command.m_CMD = Convert .ToByte(textReader.ReadElementString("CMD"),16);
command.m_Feature = Convert .ToByte(textReader.ReadElementString("Feature"),16);
textReader.ReadEndElement(); //</command>
m_ATACommands.Add(command);
}
}
catch (Exception ex)
{
//</ATAPassThrough> TODO: this is an ugly fix come up with something better later
textReader.ReadEndElement();
//cUtils.DisplayError(ex.Message);
}
plik xml:
<ATAPassThrough>
<Command>
<Name>Smart</Name>
<CMD>B0</CMD>
<Feature>D0</Feature>
</Command>
<Command>
<Name>Identify</Name>
<CMD>B1</CMD>
<Feature>D0</Feature>
</Command>
.
.
.
.
</ATAPassThrough>
Dowolny powód, dla którego nie używasz [XmlDocument] (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx) lub [XDocument] (http://msdn.microsoft .com/en-us/library/system.xml.linq.xdocument.aspx) (preferowane, jeśli masz dostęp do .Net 3.5)? –
nie tylko go nie znasz – yawnobleix
Czy możesz pominąć przykład twojego pliku XML? –