解析控制在aspx文件,并将其转换为xml [英] Parse controls in an aspx file and convert it to xml

查看:130
本文介绍了解析控制在aspx文件,并将其转换为xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过aspx文件解析(从磁盘,不呈现在浏览器上的一个),使所有的服务器侧asp.net控制$ P $页面上psent的列表,然后创建一个XML从它的文件。这将是做到这一点的最好方法是什么?此外,是否有任何可用的库呢?

I need to parse through the aspx file (from disk, and not the one rendered on the browser) and make a list of all the server side asp.net controls present on the page, and then create an xml file from it. which would be the best way to do it? Also, are there any available libraries for this?

有关例如,如果我的aspx文件中包含

For eg, if my aspx file contains

< ASP:标签ID =LBL1=服务器文本=嗨>< / ASP:标签>

我的xml文件将

<控制与GT;结果
<&ID GT;&LBL1 LT; / ID>结果
< RUNAT>服务器16; / RUNAT>结果
<文本>喜< /文本>结果
< /控制>

推荐答案

XML解析器想不明白的ASP指令:<%@<%=等等

Xml parsers wouldn't understand the ASP directives: <%@ <%= etc.

您可能会最好使用正规的前pressions要做到这一点,有可能在3个阶段。

You'll probably best to use regular expressions to do this, likely in 3 stages.


  1. 匹配从整个页面的标签元素。​​

  2. 对于每个标签,匹配的标签和控制型。

  3. 对于每一个标签的匹配(2)匹配任何属性。

所以,从顶部开始,我们可以使用下面的正则表达式:

So, starting from the top, we can use the following regex:

(?<tag><[^%/](?:.*?)>)

这将匹配没有 - 的任何标记;%和&lt; /而这样做懒洋洋地(我们不想贪前pressions,我们将无法正确读取的内容)。下面可以匹配的:

This will match any tags that don't have <% and < / and does so lazily (we don't want greedy expressions, as we won't read the content correctly). The following could be matched:

<asp:Content ID="ph_PageContent" ContentPlaceHolderID="ph_MainContent" runat="server">
<asp:Image runat="server" />
<img src="/test.png" />

对于每个被俘虏的标签,我们要然后提取标记和类型:

For each of those captured tags, we want to then extract the tag and type:

<(?<tag>[a-z][a-z1-9]*):(?<type>[a-z][a-z1-9]*)

创建一个名为捕获组,使这更容易,这将使我们能够方便地提取标签和类型。这将只匹配服务器变量,因此标准的HTML标签将在这一点上被删除。

Creating named capture groups makes this easier, this will allow us to easily extract the tag and type. This will only match server tags, so standard html tags will be dropped at this point.

<asp:Content ID="ph_PageContent" ContentPlaceHolderID="ph_MainContent" runat="server">

将产生:

{ tag = "asp", type = "Content" }

使用相同的标记,我们可以匹配任何属性:

With that same tag, we can then match any attributes:

(?<name>\S+)=["']?(?<value>(?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?

其中产量:

{ name = "ID", value = "ph_PageContent" },
{ name = "ContentPlaceHolderID", value = "ph_MainContent" },
{ name = "runat", value = "server" }

所以把所有在一起,我们可以创建一个快捷功能,可以为我们创建一个如下:

So putting that all together, we can create a quick function that can create an XmlDocument for us:

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;
}

生成的文件看起来是这样的:

The resultant document could look like this:

<controls>
  <control>
    <tag>asp</tag>
    <type>Content</type>
    <attributes>
      <ID>ph_PageContent</ID>
      <ContentPlaceHolderID>ph_MainContent</ContentPlaceHolderID>
      <runat>server</runat>
    </attributes>
  </control>
</controls>

希望帮助!

这篇关于解析控制在aspx文件,并将其转换为xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆