如何使用C#减少以下代码 [英] How to reduce the following code using C#

查看:63
本文介绍了如何使用C#减少以下代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XMLtextreader我想要循环大约16次。

所以读者工作16次消耗很多时间如何减少上面的代码





I am using XMLtextreader I want to loop through about 16 times.
So the reader works for 16 times consuming much time How to reduce the above code


int []c=new int[28];
                    for (int b = 0; b < diff.Count; b++)
                    {
                        using (XmlTextReader reader = new XmlTextReader(filename))
                        {
                            bool isInside = false;
                            double x1 = 0; double y1 = 0; double z1 = 0; double x2 = 0; double y2 = 0; double z2 = 0;
                            while (reader.Read())
                            {
                                reader.WhitespaceHandling = WhitespaceHandling.None;
                                
                                if (!isInside && reader.LocalName == "Child" && reader.GetAttribute("id") == diff[b].Key && reader.GetAttribute("type") == lstGivenString[0])
                                {
                                    isInside = true;
                                   c[0]++;
                                }
                                else if (!isInside && reader.LocalName == "Child" && reader.GetAttribute("id") == diff[b].Key && reader.GetAttribute("type") == lstGivenString[1])
                                {
                                    isInside = true;
                                    c[1]++;
                                }
                                else if (!isInside && reader.LocalName == "Child" && reader.GetAttribute("id") == diff[b].Key && reader.GetAttribute("type") == lstGivenString[2])
                                {
                                    isInside = true;
                                   c[2]++;
                                }
}
}
}

推荐答案

替换 isInside = true; by break;

所有if语句都测试! isInside 首先,因此我猜你实际上想要在任何条件成为真之后离开子句。
Replace isInside = true; by break;
All of your if statements test for !isInside first, hence I guess you actually want to leave the while clause after any of the conditions has become true.


这篇关于如何使用C#减少以下代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆