在C#中将Foreach循环转换为Linq

时间:2015-09-22 06:23:21

标签: c# linq foreach

伙计们,我想将这些循环转换为简单的循环。 我看到了与linq相关的答案,但无法从中得出答案。这个片段还有其他可能的方法吗?

XmlDocument manifestXmlFile = new XmlDocument();
manifestXmlFile.Load(manifestFileName);

foreach (XmlNode rules in manifestXmlFile.DocumentElement.ChildNodes)
{
    foreach (XmlNode ruleNode in rules)
    {
        foreach (XmlNode childNodeAttributes in ruleNode)
        {
            foreach (XmlNode childNodeAttrib in childNodeAttributes.ChildNodes)
            {
                XmlElement ruleElement = (XmlElement)ruleNode;
                foreach (XmlNode childNodeConditions in childNodeAttrib.ChildNodes)
                {
                    foreach (XmlNode childNodeCond in childNodeConditions.ChildNodes)
                    {
                        if (childNodeCond.Name.ToUpper() == "CONDITION")
                        {
                            if (childNodeCond.Attributes["type"].Value.ToUpper() == "HEALTHY")
                            {
                                string ruleId = ruleElement.Attributes["ruleid"].Value;
                                string attributeName = childNodeAttrib.Attributes["name"].Value;
                                string attributeType = childNodeAttrib.Attributes["type"].Value;
                                string condTypeValue = childNodeCond.Attributes["type"].Value;
                                string operatorValue = childNodeCond.Attributes["operator"].Value;
                                string healthyConditionValue = childNodeCond.FirstChild.InnerText;

                                var guid = new Guid(ruleId);

                                //Conversion of enum types
                                PsmsOperator psmsOperator = (PsmsOperator)Enum.Parse(typeof(PsmsOperator), operatorValue, true);
                                TypeCode psmsAttributeType = (TypeCode)Enum.Parse(typeof(TypeCode), attributeType, true);

                                Rule rule = new Rule(guid, attributeName, healthyConditionValue, psmsOperator);

                                Rule(attributes, guid);
                           }
                        }
                    }
                }
            }
        }
    }
}

除此之外还有其他更好的方法吗?

3 个答案:

答案 0 :(得分:4)

使用XPath选择所需的节点。

foreach (var node in manifestXmlFile.SelectNodes(@"//condition[@type = 'healthy']")
{
 ...
}

答案 1 :(得分:0)

嗯..它可以做到只因为你可以,但它的丑陋:) (积分转到R#):

foreach (Rule rule in from XmlNode rules in manifestXmlFile.DocumentElement.ChildNodes 
                        from XmlNode ruleNode in rules 
                        from XmlNode childNodeAttributes in ruleNode 
                        from XmlNode childNodeAttrib in childNodeAttributes.ChildNodes 
                        let ruleElement = (XmlElement) ruleNode 
                        from XmlNode childNodeConditions in childNodeAttrib.ChildNodes 
                        from XmlNode childNodeCond in childNodeConditions.ChildNodes
                        where childNodeCond.Name.ToUpper() == "CONDITION" 
                        where childNodeCond.Attributes["type"].Value.ToUpper() == "HEALTHY" 
                        let ruleId = ruleElement.Attributes["ruleid"].Value 
                        let attributeName = childNodeAttrib.Attributes["name"].Value 
                        let attributeType = childNodeAttrib.Attributes["type"].Value 
                        let condTypeValue = childNodeCond.Attributes["type"].Value 
                        let operatorValue = childNodeCond.Attributes["operator"].Value 
                        let healthyConditionValue = childNodeCond.FirstChild.InnerText 
                        let guid = new Guid(ruleId) 
                        let psmsOperator = (PsmsOperator) Enum.Parse(typeof (PsmsOperator), operatorValue, true) 
                        let psmsAttributeType = (TypeCode) Enum.Parse(typeof (TypeCode), attributeType, true) 
                        select new Rule(guid, attributeName, healthyConditionValue, psmsOperator))
{
    //do something with rule varialbe
}

答案 2 :(得分:0)

可能会这样做

XElement root = XElement.Load(manifestFileName);
root.Element("RootElement").Elements("rules").Elements("ruleNode").Elements("CONDITION").All<XElement>(xe =>
{
    if(xe.Attribute("type").Value.ToUpper() == "HEALTHY")
    {
        //do
    }
}