Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 这个代码会导致无限循环吗_C# - Fatal编程技术网

C# 这个代码会导致无限循环吗

C# 这个代码会导致无限循环吗,c#,C#,我正在调查我申请中的一些案件。我有以下代码来读取XML元素,目的是跳过触发异常的元素: bool res = true; // Start parsing the XML file using (XmlReader reader = XmlReader.Create(xmlFilePath)) { while (res) { try { // Read res = reader.Read();

我正在调查我申请中的一些案件。我有以下代码来读取XML元素,目的是跳过触发异常的元素:

bool res = true;
// Start parsing the XML file
using (XmlReader reader = XmlReader.Create(xmlFilePath))
{
    while (res)
    {
        try
        {
            // Read 
            res = reader.Read();
            if (res == false)
                break;

            // Is this XML element?
            if ((reader.NodeType == XmlNodeType.Element))
            {
                // What is its name?
                if (reader.Name == "Test")
                {                        

                  //...
                }
            }
        }
        catch (Exception ex)
        {
            // Exception - continue to reading other XML elements
            HelperMethods.AppendToLogFile("Method: TryRetrieveRestartReasons. " + terminalName + " " + "Filename: " + xmlFilePath + "Ex: " + ex.Message, LogType.Error);
            // HelperMethods.TryInsertParsingExceptionToDatabase(terminalName, Path.GetFileNameWithoutExtension(xmlFilePath) + ".log", ex.Message);
        }
    }
}
我的问题是,你们认为上面的代码片段会导致无限循环吗? 否则,如何继续编写代码,跳过在XML文件中生成异常的那些行

我需要从确信reader.Read()是否会的人那里知道答案 在我上面的代码中总是返回false

它不会总是返回
false

由于可以抛出异常(例如,当XML无效时),因此
try-catch
reader.Read
存在问题

如果
Read
抛出异常,那么
t
永远不会变成
false
,您将得到无限循环。循环的第一行应该是
while(reader.Read())


下次我给Reader.Read打电话时,它返回FALSE

你不能肯定。
即使现在/对于某些XML示例是这样,这也是实现细节。这种行为没有记录在案。我不会依赖它。

考虑这样一种场景:
reader.Read()
成功读取一次,然后每次都抛出异常

第一次调用行
res=reader.Read()
时,
res
设置为
true
。然后,每次之后
reader.Read()
都会抛出一个异常。关键是当它抛出异常时,
res
不会发生任何事情。它保留其值
true

因此,捕获异常,
res
保持
true
,循环重新开始。
while
表示“是,
res
true
。继续。”然后,在
res
可以设置为任何其他内容之前,会引发另一个异常


因此,只要它是
reader.Read()
抛出异常,并且您至少有一个成功的
Read()
调用,那么您将得到一个无限循环。

什么是
reader
以及
Read()
做什么?如果它永远不等于false,那么肯定有一个loop@Sayse:XMLReaderIt是完全合理的循环,直到stringreader/filereader/xmlreader等用尽。我故意这样设计,目的是为了在某些XML元素出现异常时继续读取其他元素。但正如我所说的,如果下次执行reader.Read()时出现异常,它将返回false,所以我希望听到了解该问题的人的回答this@user300455看这里:。如果XmlReader不能解析XML,它将引发XmlException,这样您就可以得到包含无效XML数据的无限循环。(注意:异常后我仍然会转到while循环-不是吗(因为它的设计是这样的),然后异常后通常是在我测试reader时。Read返回false)@user300455:更新了答案。依赖于实现细节是一个坏主意。“下次我调用Reader.Read时,它返回FALSE”——我认为这是正确的捕获?如果这种行为总是发生,那么我的代码将是正确的,不会导致无限循环,对吗?顺便问一下,如果在某个点遇到异常(我的代码试图实现的),如何创建尝试继续读取其他XML元素的解决方案。我的观点是要么完全读取有效的XML,或者扔掉一个无效的。我看不出解析无效XML有什么好处——这是一项相当复杂的任务,结果不可预测。