XML解析器想不明白的ASP指令:<%@ <%=等
你可能最好使用正则表达式来做到这一点,可能会在3个阶段。
- 匹配整个页面的任何标签元素。
- 对于每个标签,匹配标签和控制类型。
- 对于每个匹配(2)的标签,匹配任何属性。
所以,从顶部开始,我们可以使用下面的正则表达式:
(?<tag><[^%/](?:.*?)>)
这将匹配没有<%和<任何标签/和不那么懒洋洋地(我们不”不要贪婪的表达,因为我们不会正确读取内容)。下面可以匹配:
<asp:Content ID="ph_PageContent" ContentPlaceHolderID="ph_MainContent" runat="server">
<asp:Image runat="server" />
<img src="/test.png" />
对于每一个被俘虏的标签,我们要然后提取标签和类型:
<(?<tag>[a-z][a-z1-9]*):(?<type>[a-z][a-z1-9]*)
创建命名捕捉组,使这更容易,这将让我们轻松提取标签和类型。这只会匹配服务器标签,因此标准的html标签将在此时被删除。
<asp:Content ID="ph_PageContent" ContentPlaceHolderID="ph_MainContent" runat="server">
将产生:
{ tag = "asp", type = "Content" }
用相同的标签,我们可以匹配任何属性:
(?<name>\S+)=["']?(?<value>(?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?
其中产量:
{ name = "ID", value = "ph_PageContent" },
{ name = "ContentPlaceHolderID", value = "ph_MainContent" },
{ name = "runat", value = "server" }
所以把所有的在一起,我们可以创建一个快速的功能能为我们创造一个XmlDocument:
public XmlDocument CreateDocumentFromMarkup(string content)
{
if (string.IsNullOrEmpty(content))
throw new ArgumentException("'content' must have a value.", "content");
RegexOptions options = RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase;
Regex tagExpr = new Regex("(?<tag><[^%/](?:.*?)>)", options);
Regex serverTagExpr = new Regex("<(?<tag>[a-z][a-z1-9]*):(?<type>[a-z][a-z1-9]*)", options);
Regex attributeExpr = new Regex("(?<name>\\S+)=[\"']?(?<value>(?:.(?![\"']?\\s+(?:\\S+)=|[>\"']))+.)[\"']?", options);
XmlDocument document = new XmlDocument();
XmlElement root = document.CreateElement("controls");
Func<XmlDocument, string, string, XmlElement> creator = (document, name, value) => {
XmlElement element = document.CreateElement(name);
element.InnerText = value;
return element;
};
foreach (Match tagMatch in tagExpr.Matches(content)) {
Match serverTagMatch = serverTagExpr.Match(tagMatch.Value);
if (serverTagMatch.Success) {
XmlElement controlElement = document.CreateElement("control");
controlElement.AppendChild(
creator(document, "tag", serverTagMatch.Groups["tag"].Value));
controlElement.AppendChild(
creator(document, "type", serverTagMatch.Groups["type"].Value));
XmlElement attributeElement = document.CreateElement("attributes");
foreach (Match attributeMatch in attributeExpr.Matches(tagMatch.Value)) {
if (attributeMatch.Success) {
attributeElement.AppendChild(
creator(document, attributeMatch.Groups["name"].Value, attributeMatch.Groups["value"].Value));
}
}
controlElement.AppendChild(attributeElement);
root.AppendChild(controlElement);
}
}
return document;
}
得到的文件看起来是这样的:
<controls>
<control>
<tag>asp</tag>
<type>Content</type>
<attributes>
<ID>ph_PageContent</ID>
<ContentPlaceHolderID>ph_MainContent</ContentPlaceHolderID>
<runat>server</runat>
</attributes>
</control>
</controls>
希望帮助!
你说的意思是“本”在你的最后一个问题(“此外,是否有这方面的任何可用的库?”)?解析XML( - >是,有(堆)库)或在(有效的)ASPX文件中找到控件( - >不太确定是否有库)。 – scherand 2010-06-04 08:27:13
“我”的意思是,任何已有的库都可以直接完成这个任务,而不是我为它编写逻辑。 – Ubaid 2010-06-04 08:36:30