C# HtmlTextWriter to String - 我是否忽略了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1008551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HtmlTextWriter to String - Am I overlooking something?
提问by Adam McKee
Perhaps I'm going about this all wrong (and please tell me if I am), but I'm hitting my head against a wall with something that seems like a really simple concept.
也许我在这一切都错了(如果我错了,请告诉我),但我正在用看起来非常简单的概念撞墙。
This Render override is coming from a User Control.
此渲染覆盖来自用户控件。
protected override void Render(HtmlTextWriter writer)
{
string htmlAboutToBeRendered = writer.GetWhatHasBeenWrittenToTheWriterSoFar();
// Do something nefarious, yet unrelated with htmlAboutToBeRendered
}
This seems like a there would be an obvious way to do this, but I can't seem to find it.
这似乎有一种明显的方法可以做到这一点,但我似乎找不到它。
Can anyone shed some light on this for me, please?
任何人都可以为我解释一下吗?
采纳答案by TheTXI
Try this:
尝试这个:
protected override void RenderContents(HtmlTextWriter output)
{
StringBuilder sb = new StringBuilder();
HtmlTextWriter htw = new HtmlTextWriter(new System.IO.StringWriter(sb,
System.Globalization.CultureInfo.InvariantCulture));
foreach (Control ctrl in Controls)
{
ctrl.RenderControl(htw);
}
string strContents = sb.ToString();
}
}
回答by Robert Horvick
You can derive from HttpTextWriter and override OnTagRender, OnAttributeRender and OnStyleRender methods and modify the tags as they are rendered. The MSDN docs for OnTagRender show a sample where the tag is modified during rendering:
您可以从 HttpTextWriter 派生并覆盖 OnTagRender、OnAttributeRender 和 OnStyleRender 方法并在呈现标签时修改标签。OnTagRender 的 MSDN 文档显示了在渲染期间修改标记的示例:
http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.ontagrender.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.ontagrender.aspx
回答by Mark
Alternate method using relfection:
使用 relfection 的替代方法:
private string GetString(HtmlTextWriter writer)
{
// the flags to see the internal properties of the writer
System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.Default;
flags |= System.Reflection.BindingFlags.NonPublic;
flags |= System.Reflection.BindingFlags.Instance;
flags |= System.Reflection.BindingFlags.FlattenHierarchy;
flags |= System.Reflection.BindingFlags.Public;
// get the information about the internal TextWriter object
System.Reflection.FieldInfo baseWriter = writer.GetType().GetField("writer", flags);
// use that info to create a StringWriter
System.IO.StringWriter reflectedWriter = (System.IO.StringWriter)baseWriter.GetValue(writer);
// now we get a StringBuilder!
StringBuilder builder = reflectedWriter.GetStringBuilder();
return builder.ToString();
}
Then it's a simple matter of re-creating the HtmlTextWriter using the string and a StringBuilder.
然后是使用字符串和 StringBuilder 重新创建 HtmlTextWriter 的简单问题。
This was built using a couple clues I picked up from a conversation between Tom Spink and Rotseyon EggheadCafe
这是使用我从Tom Spink 和 Rotsey在 EggheadCafe 上的对话中获得的一些线索构建的