[英]Convert Foreach Loop into Linq in C#
伙计们,我想将这些循环转换为简单循环。 我看到了与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);
}
}
}
}
}
}
}
}
除了此或linq,还有其他更好的方法吗?
使用XPath选择所需的节点。
foreach (var node in manifestXmlFile.SelectNodes(@"//condition[@type = 'healthy']")
{
...
}
嗯..之所以可以这样做,是因为您可以做到,但是它很丑陋:)(信用转到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
}
也许这可以解决问题
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.