Also ich lese eine XML-Datei mit unbekannter Länge und liest jedes Element in eine Listenstruktur. Jetzt, wenn ich am Ende der Datei angelangt bin, lese ich weiter, das verursacht eine Ausnahme. Gerade jetzt fange ich diese Ausnahme und fahre mit meinem Leben fort, aber gibt es einen saubereren Weg, dies zu tun?So behandeln Sie das Ende der Datei beim Lesen der XML-Datei
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);
}
XML-Datei:
<ATAPassThrough>
<Command>
<Name>Smart</Name>
<CMD>B0</CMD>
<Feature>D0</Feature>
</Command>
<Command>
<Name>Identify</Name>
<CMD>B1</CMD>
<Feature>D0</Feature>
</Command>
.
.
.
.
</ATAPassThrough>
Jeder Grund, warum Sie [XmlDocument] nicht verwenden (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx) oder [XDocument] (http://msdn.microsoft.com) .com/de-us/library/system.xml.linq.xdocument.aspx) (bevorzugt, wenn Sie Zugriff auf .Net 3.5 haben)? –
nein, nur nicht vertraut mit ihm – yawnobleix
Können Sie ein Beispiel für Ihre XML-Datei? –