Quindi sto leggendo un file xml con lunghezza sconosciuta e leggendo ogni elemento in una struttura di lista. In questo momento, una volta arrivato alla fine del file, continuo a leggere, questo causa un'eccezione. In questo momento ho appena preso questa eccezione e continuo con la mia vita, ma c'è un modo più pulito per fare questo?Come gestire la fine del file durante la lettura del file 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);
}
file 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>
Qualsiasi motivo non si sta utilizzando [XmlDocument] (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx) o [XDocument] (http://msdn.microsoft .com/it/us/library/system.xml.linq.xdocument.aspx) (preferibile se si ha accesso a .Net 3.5)? –
non si ha familiarità con esso – yawnobleix
Si può superare un esempio del file XML? –