06.09.2019, 16:35
#39858519
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
|
Участник
Сообщения: 2 702
Рейтинг:
0
/ 0
|
|
|
|
Скажите пожалуйста, можно ли создать XML по такому исходнику?
Особенно не понятна эта строка:
1.
XmlNodeList currentRulesDef = xRoot.SelectNodes(string.Format("RuleDef[@OID='{0}']", OID));
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196.
protected void btnUpload_Click(object sender, EventArgs e)
{
lblStatus.Visible = false;
List<Rule_Error> errorList = new List<Rule_Error>();
if (fuAddQuery.HasFile)
{
string folderPath = Request.PhysicalApplicationPath + "Data/Rules/";
if (!Directory.Exists(folderPath))
{
try
{
Directory.CreateDirectory(folderPath);
}
catch (Exception error)
{
Response.Write(error.Message);
return;
}
}
String fileExtension = Path.GetExtension(fuAddQuery.FileName).ToLower();
string filePath = "";
if (fileExtension == ".xml")
{
bool uploaded = false;
try
{
filePath = folderPath + fuAddQuery.FileName;
if (File.Exists(filePath))
{
errorList.Add(new Rule_Error() { ErrorMessage="Файл уже существует" });
LoadErrors(errorList);
return;
}
fuAddQuery.PostedFile.SaveAs(filePath);
uploaded = true;
}
catch (Exception ex)
{
errorList.Add(new Rule_Error() { ErrorMessage = "Файл не загружен. <br/>" + ex.Message });
LoadErrors(errorList);
return;
}
if (uploaded)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(filePath); //загружаем документ
}
catch(Exception error)
{
errorList.Add(new Rule_Error() { ErrorMessage = error.Message });
LoadErrors(errorList);
File.Delete(filePath);
return;
}
XmlElement xRoot = xDoc.DocumentElement;
XmlNodeList ruleAssigments = xRoot.SelectNodes("RuleAssignment"); //получаем список RuleAssignment
XmlNodeList ruleDef = xRoot.SelectNodes("RuleDef"); //получаем список RuleDef
//проходим по всем RuleAssignment
for(int i =0;i<ruleAssigments.Count;i++)
{
XmlNode item = ruleAssigments[i];
//выясняем Таргет
string target = item.SelectSingleNode("Target").InnerText;
Target t = RuleParse.TargetParse(errorList, target,"",i+1);
XmlNodeList ruleRef = item.SelectNodes("RuleRef"); //получаем список RuleRef
foreach (XmlNode rule in ruleRef)
{
var newRule = new Models.Rule();
newRule.EventID = t.EventID;
newRule.CRFID = t.CRFID;
newRule.GroupID = t.GroupID;
newRule.ItemID = t.ItemID;
newRule.AddedDate = DateTime.Now;
string OID = rule.Attributes["OID"].InnerText; //OID
List<Models.Rule> thisOIDs = RR.GetManyByFilter(x=> x.OID==OID).ToList();
if(thisOIDs.Count>0)
{
errorList.Add(new Rule_Error()
{
ErrorMessage = "Уже существуют правила с указанным OID",
RuleAssignmentNumber = i + 1,
OID = OID
});
continue;
}
XmlNode discrepancyNoteAction = rule.SelectSingleNode("DiscrepancyNoteAction");
bool ifExpressionEvaluates = //ifExpressionEvaluates
discrepancyNoteAction.Attributes["IfExpressionEvaluates"].InnerText.ToLower() == "true";
string message = discrepancyNoteAction.SelectSingleNode("Message").InnerText; //ErrorMessage
newRule.ErrorMessage = message;
newRule.Target = target;
newRule.OID = OID;
newRule.IfExpressionEvaluates = ifExpressionEvaluates;
XmlNodeList currentRulesDef = xRoot.SelectNodes(string.Format("RuleDef[@OID='{0}']", OID));
string ruleName = "";
string expression = "";
if (currentRulesDef.Count != 1)
{
errorList.Add(new Rule_Error()
{
ErrorMessage = "Не удалось найти RuleDef с указанным OID",
RuleAssignmentNumber = i+1,
OID = OID
});
}
else
{
ruleName = currentRulesDef[0].Attributes["Name"].InnerText;
expression = currentRulesDef[0].SelectSingleNode("Expression").InnerText;
if (errorList.Count > 0)
continue;
if (expression.Length >= 5)
{
expression = Regex.Replace(expression, @"( eq )", " == ", RegexOptions.IgnoreCase);
expression = Regex.Replace(expression, @"( ne )", " != ", RegexOptions.IgnoreCase);
expression = Regex.Replace(expression, @"( lt )", " < ", RegexOptions.IgnoreCase);
expression = Regex.Replace(expression, @"( lte )", " <= ", RegexOptions.IgnoreCase);
expression = Regex.Replace(expression, @"( gt )", " > ", RegexOptions.IgnoreCase);
expression = Regex.Replace(expression, @"( gte )", " >= ", RegexOptions.IgnoreCase);
List<IToken> tokens = RuleParse.GetTokenList(expression);
if(tokens.Count==0)
{
errorList.Add(new Rule_Error()
{
ErrorMessage = "При разборе выражения произошла ошибка. Выражение: " + expression,
RuleAssignmentNumber = i + 1,
OID = OID
});
}
RuleParse.UpdateCrfTokens(tokens,t, errorList,i+1,OID);
if (errorList.Count > 0)
continue;
if(!RuleParse.TestTokens(tokens))
{
errorList.Add(new Rule_Error()
{
ErrorMessage = "Не верно указано выражение для проверки: " + expression,
RuleAssignmentNumber = i + 1,
OID = OID
});
}
newRule.Name = ruleName;
newRule.Expression = expression;
newRule.Tokens = new List<Models.Token>();
foreach (IToken iT in tokens)
newRule.Tokens.Add(new Models.Token() { Type = iT.Type, Value = iT.Value });
}
else
{
errorList.Add(new Rule_Error()
{
ErrorMessage = "Не верно указано выражение для проверки: "+expression,
RuleAssignmentNumber = i+1,
OID = OID
});
}
}
if(errorList.Count==0)
RR.Create(newRule);
}
File.Delete(filePath);
}
if (errorList.Count == 0)
{
lblStatus.Visible = true;
RR.Save();
}
}
}
}
else
{
errorList.Add(new Rule_Error() { ErrorMessage = "Не выбран файл для загрузки" });
}
LoadErrors(errorList);
}
Модератор: Учимся использовать тэги оформления кода - FAQ
Заранее благодарен.
|
|