पढ़ते समय फ़ाइल के अंत को कैसे संभालें, इसलिए मैं अज्ञात लंबाई के साथ एक XML फ़ाइल पढ़ रहा हूं और प्रत्येक तत्व को सूची संरचना में पढ़ रहा हूं। एक बार जब मैं फ़ाइल के अंत तक पहुंच जाता हूं तो मैं पढ़ना जारी रखता हूं, इससे अपवाद होता है। अभी मैं बस इस अपवाद को पकड़ता हूं और अपने जीवन के साथ जारी रहता हूं लेकिन क्या ऐसा करने का एक साफ तरीका है?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 फ़ाइल:
<ATAPassThrough>
<Command>
<Name>Smart</Name>
<CMD>B0</CMD>
<Feature>D0</Feature>
</Command>
<Command>
<Name>Identify</Name>
<CMD>B1</CMD>
<Feature>D0</Feature>
</Command>
.
.
.
.
</ATAPassThrough>
कोई कारण नहीं है कि आप [XmlDocument] (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx) या [XDocument] (http://msdn.microsoft) का उपयोग नहीं कर रहे हैं .com/en-us/library/system.xml.linq.xdocument.aspx) (अगर आपके पास .NET 3.5 तक पहुंच है तो प्रीफर किया गया है)? –
नहीं बस इसके साथ परिचित नहीं – yawnobleix
क्या आप अपनी एक्सएमएल फाइल का एक उदाहरण चिपका सकते हैं? –