【发布时间】:2017-12-19 05:07:31
【问题描述】:
我正在将例程从 C++ 移植到 C#,但很难理解移植失败的原因:
我有一个字符串数组,其中包含我正在尝试删除的内容。
string[] aryLines = File.ReadAllLines(mstrFilename);
数组包含以下内容:
aryLines = new[]
{
"<?xml version=\"1.0\" encoding=\"utf-8\"?>",
"<!--",
"",
" File:\t\tuif.xml, User Interface",
" Notes:\tThis file contains the application layout and includes",
"\t\tfor other files defining the application look and functionality.",
"",
" Node:\t\tuif, Root container node",
" Attributes:\tid\t\t: Unique node identifier",
"\t\tcameras\t\t: Initial camera set-up",
" \t\tcolor_bg\t: Application background colour:",
"\t\t\t\t\tAlpha, Red, Green, Blue",
"\t\theight\t\t: Height of the application container",
"\t\twidth\t\t: Width of the appplication container\t\t",
"",
" Node:\t\tinclude, Includes another XML file",
" Attributes:\tname\t\t: Encoded path to XML file to include",
"",
" History:\t2017/09/11 Created by Simon Platten",
"// -->"
};
我有一个方法应该删除 cmets,找到第一次出现的 <!-- 和匹配的 -->,然后它将删除中间的所有内容。问题是,虽然它找到了<!--,但它没有找到-->,我不明白为什么。
private static readonly string msrostrCmtClose = "-->";
private static readonly string msrostrCmtOpen = "<!--";
int intOpen = 0;
while((intOpen = Array.IndexOf(aryLines, msrostrCmtOpen, intOpen)) >= 0)
{
//Opening marker located, look for closing marker
int intClose = Array.IndexOf(aryLines, msrostrCmtClose, intOpen);
if ( intClose < intOpen )
{
//Shouldn't get here!
continue;
}
Console.WriteLine(intOpen);
}
上面的套路不完整,但是在调试器中看intClose总是-1,为什么?
【问题讨论】:
-
由于 cmets 可以跨越多行,您可能应该将文件读入一个长字符串,而不是一系列独立的“行”。那么你的逻辑会更清晰。
-
因为你在第 19 行有 // --> Array.IndexOf 不是 string.Contains
-
Array.IndexOf对数组中的元素进行完全匹配。在您的情况下,它正在对字符串的全部内容进行完全匹配。您似乎希望它做的是每个字符串元素的部分匹配。 -
您最好使用支持 DOM 的 XML 解析器。然后你可以只寻找评论节点并删除它们。 :P
-
@SPlatten:如果是 XML,那么 XML 解析器可以解析它。如果没有一些帮助,可能无法完全理解它,但 XML 解析器不会在格式良好的 XML 上窒息。
标签: c#