Vì vậy, tôi đang đọc tệp xml có độ dài không xác định và đọc từng phần tử vào cấu trúc danh sách. Ngay bây giờ một khi tôi nhận được vào cuối của tập tin tôi tiếp tục đọc, điều này gây ra một ngoại lệ. Ngay bây giờ tôi chỉ bắt ngoại lệ này và tiếp tục với cuộc sống của tôi nhưng có cách nào sạch hơn để làm điều này không?Cách xử lý hết tệp khi đọc tệp 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);
}
xml file:
<ATAPassThrough>
<Command>
<Name>Smart</Name>
<CMD>B0</CMD>
<Feature>D0</Feature>
</Command>
<Command>
<Name>Identify</Name>
<CMD>B1</CMD>
<Feature>D0</Feature>
</Command>
.
.
.
.
</ATAPassThrough>
Bất kỳ lý do nào bạn không sử dụng [XmlDocument] (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx) hoặc [XDocument] (http://msdn.microsoft) .com/vi-us/library/system.xml.linq.xdocument.aspx) (được ưu tiên nếu bạn có quyền truy cập vào .Net 3.5)? –
nope không quen thuộc với nó – yawnobleix
Bạn có thể vượt qua một ví dụ về tệp XML của bạn không? –