我正在文本框中制作一种自定义脚本语言。
例如:
<NAME1>
,<NAME2>
或<FAM>
并且在执行时,标记会转换为来自字段的文件,数据库或用户输入的数据。
在给定的例子中,结果是:
Elliot, John or Gleave.
现在这很好。我得出结论,我需要if声明。
以下是一些例子:
<IF NAME1="George">
<DoStuff>
<ENDIF>
我正在使用的方法是从字符串中查找索引并对它们进行子串。 你能给我一些想法或指导如何制作自定义if语句吗?
编辑:我设法制作了一个方法来做我想做的事情。所以我在这里分享它,如果有人想编辑,建议或等等。请告诉我。
public static string ReplaceIfStatement(string script, CResultData resultData)
{
string workingScript = script;
string manipulatedScript = workingScript.Clone() as string; // storing the script in temp variable for further checks
workingScript = CStringFunctions.ReplaceAll(workingScript, "\r", string.Empty); // ReplaceAll uses the string.Replace() method
workingScript = CStringFunctions.ReplaceAll(workingScript, "\t", string.Empty);
string ifStartFieldName = "<IF ";
string ifEndFieldName = "<ENDIF>";
workingScript = CStringFunctions.ReplaceAll(workingScript, "<IF\n", ifStartFieldName);
bool repeat = true;
while (repeat)
{
int ifStartIndexOpenTag = workingScript.IndexOf(ifStartFieldName);
int ifEndIndexOpenTag = workingScript.IndexOf(ifEndFieldName);
if (ifStartIndexOpenTag != -1
|| ifEndIndexOpenTag != -1)
{
int ifStartIndexCloseTag = CStringFunctions.IndexOfString(
workingScript,
">",
ifStartIndexOpenTag + ifStartFieldName.Length + 1); //IndexOfString() uses the IndexOf()
int ifEndIndexCloseTag = CStringFunctions.IndexOfString(
workingScript,
">",
ifEndIndexOpenTag + ifStartFieldName.Length + 1);
if (ifStartIndexCloseTag != -1)
{
string ifStatement = CStringFunctions.SubstrString(
workingScript,
ifStartIndexOpenTag + ifStartFieldName.Length,
ifStartIndexCloseTag - (ifStartIndexOpenTag + ifStartFieldName.Length));
ifStatement = CStringFunctions.ReplaceAll(ifStatement, " ", string.Empty);
string rightSideIfStatement = CStringFunctions.SubstrString(
ifStatement,
1,
CStringFunctions.IndexOfString(ifStatement, "=") - 1);
string leftSideIfStatement = CStringFunctions.SubstrString(
ifStatement,
CStringFunctions.IndexOfString(ifStatement, "=") + 1,
CStringFunctions.IndexOfString(ifStatement, ")")
- CStringFunctions.IndexOfString(ifStatement, "=") - 1);
CResultData ifResultData = resultData.Clone(); // here is the class containing all props, ex: NAME1, NAME2 or FAM
foreach (PropertyInfo property in ifResultData.GetType().GetProperties())
{
if (rightSideIfStatement == property.Name.ToUpper()
&& leftSideIfStatement == (string)property.GetValue(ifResultData, null)) // In this statement compare the property name in the <IF> and the <IF (statement)>
{
//if the compare is true i remove the <IF (statement)> and the <ENDIF> from the script
manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
workingScript,
ifStartIndexOpenTag,
ifStartIndexCloseTag + 1,
string.Empty);
ifEndIndexOpenTag = CStringFunctions.IndexOfString(manipulatedScript, ifEndFieldName);
ifEndIndexCloseTag = CStringFunctions.IndexOfString(
manipulatedScript,
">",
ifEndIndexOpenTag + ifStartFieldName.Length + 1);
manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
manipulatedScript,
ifEndIndexOpenTag,
ifEndIndexCloseTag + 1,
string.Empty);
workingScript = manipulatedScript;
return workingScript;
}
}
if (manipulatedScript != null && manipulatedScript.Length == workingScript.Length) // When the IF statement is false
//here I compare the parameter script with the temp script if there are changes to remove from the script begging from <IF (statement)> to <ENDIF>
{
manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
workingScript,
ifStartIndexOpenTag,
ifEndIndexCloseTag + 1,
string.Empty);
workingScript = manipulatedScript;
return workingScript;
}
}
else
{
repeat = false;
}
}
else
{
repeat = false;
}
}
workingScript = manipulatedScript;
return workingScript;
}
答案 0 :(得分:3)
如果您打算使用上面提供的标记样式编写自定义脚本语言,那么您也可以使其符合XML标准。
See this example of parsing XML.
假设使用XML和上述语法,
var document = new XMLDocument();
document.Load(xmlFilePath);
var documentNodes = document.DocumentElement.SelectNodes("path/from/root/to/stuff/of/interest");
foreach (var node in documentNodes)
{
var name1NodeValue = node.SelectSingleNode("NAME1").InnerText;
var ifNode = node.SelectSingleNode("IF");
if (ifNode.Attributes["NAME1"].Value.Equals(name1NodeValue) {
//Look for the node of interest. I just hard-coded doStuff for the sake of brevity.
doStuff();
}
}
不是一个完美的例子,但应该足以让你前进。