本文整理汇总了C#中System.IO.StringWriter.Flush方法的典型用法代码示例。如果您正苦于以下问题:C# StringWriter.Flush方法的具体用法?C# StringWriter.Flush怎么用?C# StringWriter.Flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.StringWriter
的用法示例。
在下文中一共展示了StringWriter.Flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseHtml
// Creates XmlDocument from html content and return it with rootitem "<root>".
public static XmlDocument ParseHtml(string sContent)
{
StringReader sr = new StringReader("<root>" + sContent + "</root>");
SgmlReader reader = new SgmlReader();
reader.WhitespaceHandling = WhitespaceHandling.All;
reader.CaseFolding = Sgml.CaseFolding.ToLower;
reader.InputStream = sr;
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTextWriter(sw);
w.Formatting = Formatting.Indented;
w.WriteStartDocument();
reader.Read();
while (!reader.EOF)
{
w.WriteNode(reader, true);
}
w.Flush();
w.Close();
sw.Flush();
// create document
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.XmlResolver = null;
doc.LoadXml(sw.ToString());
reader.Close();
return doc;
}
示例2: SessionInfoToString
/// <summary>
/// Sessions the info to string.
/// </summary>
/// <param name="sessionInfo">The session info.</param>
/// <returns></returns>
public static string SessionInfoToString(SessionInfo sessionInfo)
{
if (sessionInfo == null)
return null;
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlSerializer xml = new XmlSerializer(typeof(SessionInfo));
xml.Serialize(sw, sessionInfo);
sw.Flush();
CryptoString cry = new CryptoString(_key, CryptoString.Method.Encrypt, sb.ToString());
string ret = cry.Execute();
sb.Remove(0, sb.Length);
if (Regex.IsMatch(ret, @".*[Oo][Nn][a-zA-Z]*=*$"))
{
sessionInfo.ConnectionString += ';';
xml.Serialize(sw, sessionInfo);
sw.Flush();
cry = new CryptoString(_key, CryptoString.Method.Encrypt, sb.ToString());
ret = cry.Execute();
}
sw.Close();
return ret;
}
示例3: UrlRequestCall_ShouldBe_Profiled
public void UrlRequestCall_ShouldBe_Profiled()
{
// Arrange
var originalConsoleOut = Console.Out;
using (var writer = new StringWriter())
{
Console.SetOut(writer);
try
{
IUrlRequest profilerUrlRequest = new ProfilerUrlRequest();
// Act
var content = profilerUrlRequest.GetUrlContent("http://www.google.de");
writer.Flush();
var consoleOutput = writer.GetStringBuilder().ToString();
// Assert
Assert.IsNotNull(content);
Assert.IsNotNull(consoleOutput);
Assert.IsTrue(consoleOutput.StartsWith("Method 'string GetUrlContent(string url)', Duration (ms):"));
}
finally
{
Console.SetOut(originalConsoleOut);
}
}
}
示例4: TestBuildCode
protected void TestBuildCode(string classFileName, string xmlFileName, string contentTypeName)
{
ContentType contentType;
var expectedOutput = "";
using (var inputReader = File.OpenText(@"..\..\TestFiles\" + xmlFileName + ".xml"))
{
contentType = new ContentTypeSerializer().Deserialize(inputReader);
}
using (var goldReader = File.OpenText(@"..\..\TestFiles\" + classFileName + ".cs"))
{
expectedOutput = goldReader.ReadToEnd();
}
var configuration = CodeGeneratorConfiguration.Create();
var typeConfig = configuration.Get(contentTypeName);
typeConfig.BaseClass = "Umbraco.Core.Models.TypedModelBase";
typeConfig.Namespace = "Umbraco.CodeGen.Models";
configuration.TypeMappings.Add(new TypeMapping("Umbraco.Integer", "Int32"));
OnConfiguring(configuration, contentTypeName);
var sb = new StringBuilder();
var writer = new StringWriter(sb);
var dataTypeProvider = new TestDataTypeProvider();
var generator = new CodeGenerator(typeConfig, dataTypeProvider, CreateGeneratorFactory());
generator.Generate(contentType, writer);
writer.Flush();
Console.WriteLine(sb.ToString());
Assert.AreEqual(expectedOutput, sb.ToString());
}
示例5: ToJsonString
public static string ToJsonString(this JToken token)
{
if (token == null)
{
throw new ArgumentNullException("token");
}
// The following code is different from token.ToString(), which special-cases null to return "" instead of
// "null".
StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
try
{
using (JsonWriter jsonWriter = JsonSerialization.CreateJsonTextWriter(stringWriter))
{
token.WriteTo(jsonWriter);
jsonWriter.Flush();
stringWriter.Flush();
string result = stringWriter.ToString();
stringWriter = null;
return result;
}
}
finally
{
if (stringWriter != null)
{
stringWriter.Dispose();
}
}
}
示例6: GetGenerateProjectXSLT
public static StringReader GetGenerateProjectXSLT(string path)
{
Stream generateProjectStream;
var generateProjectXSLT = Path.Combine(path, "Build", "GenerateProject.xslt");
if (File.Exists(generateProjectXSLT))
generateProjectStream = File.Open(generateProjectXSLT, FileMode.Open);
else
generateProjectStream = GetTransparentDecompressionStream(
Assembly.GetExecutingAssembly().GetManifestResourceStream(
"Protobuild.BuildResources.GenerateProject.xslt.gz"));
using (var stream = generateProjectStream)
{
using (var writer = new StringWriter())
{
var additional = "";
var additionalPath = Path.Combine(path, "Build", "AdditionalProjectTransforms.xslt");
if (File.Exists(additionalPath))
{
using (var reader = new StreamReader(additionalPath))
{
additional = reader.ReadToEnd();
}
}
using (var reader = new StreamReader(stream))
{
var text = reader.ReadToEnd();
text = text.Replace("{ADDITIONAL_TRANSFORMS}", additional);
writer.Write(text);
writer.Flush();
}
return new StringReader(writer.GetStringBuilder().ToString());
}
}
}
示例7: ExportToExcel
private static byte[] ExportToExcel(Dictionary<string, object> model, string templatePath)
{
TemplateManager template = TemplateManager.FromFile(templatePath);
if (model != null)
{
foreach (string key in model.Keys)
{
template.SetValue(key, model[key]);
}
}
template.Functions.Add("DateToString", new TemplateFunction(DateToString));
template.Functions.Add("GetDate", new TemplateFunction(GetDate));
template.Functions.Add("GetMarket", new TemplateFunction(GetMarket));
template.Functions.Add("GetSumBuyCount", new TemplateFunction(GetSumBuyCount));
template.Functions.Add("GetSumBuyQuatity", new TemplateFunction(GetSumBuyQuatity));
template.Functions.Add("GetSumSellCount", new TemplateFunction(GetSumSellCount));
template.Functions.Add("GetSumSellQuatity", new TemplateFunction(GetSumSellQuatity));
template.Functions.Add("GetSumChange", new TemplateFunction(GetSumChange));
template.Functions.Add("GetSumDVDMTrungBinhTrenLenh", new TemplateFunction(GetSumDVDMTrungBinhTrenLenh));
template.Functions.Add("GetSumDVDBTrungBinhTrenLenh", new TemplateFunction(GetSumDVDBTrungBinhTrenLenh));
template.Functions.Add("GetSumVolume", new TemplateFunction(GetSumVolume));
template.Functions.Add("GetSumTotalValue", new TemplateFunction(GetSumTotalValue));
StringWriter writer = new StringWriter();
template.Process(writer);
writer.Flush();
byte[] data = Encoding.UTF8.GetBytes(writer.GetStringBuilder().ToString());
return data;
}
示例8: Decode
public static string Decode(string contents)
{
if (contents == null)
{
throw new ArgumentNullException("contents");
}
using (StringWriter writer = new StringWriter())
{
using (StringReader reader = new StringReader(contents))
{
string str;
while ((str = reader.ReadLine()) != null)
{
str.TrimEnd(new char[0]);
if (str.EndsWith("="))
{
writer.Write(DecodeLine(str));
}
else
{
writer.WriteLine(DecodeLine(str));
}
}
}
writer.Flush();
return writer.ToString();
}
}
示例9: RunScript
string RunScript(string source, bool execute)
{
CompilerResults results;
using (var provider = new IACodeProvider())
{
var options = new IACompilerParameters { GenerateExecutable = false, GenerateInMemory = true, Merge = true, MergeFallbackToLink = true };
results = provider.CompileAssemblyFromFile(options, source);
}
var buffer = new StringBuilder();
using (var writer = new StringWriter(buffer))
{
Console.SetOut(writer);
if (execute)
results.CompiledAssembly.EntryPoint.Invoke(null, null);
writer.Flush();
}
string output = buffer.ToString();
using (var console = Console.OpenStandardOutput())
{
var stdout = new StreamWriter(console);
stdout.AutoFlush = true;
Console.SetOut(stdout);
}
return output;
}
示例10: ExecuteHandler
public virtual string ExecuteHandler(IHttpHandler handler)
{
var writer = new StringWriter();
HttpContext.Current.Server.Execute(handler, writer, true);
writer.Flush();
return writer.GetStringBuilder().ToString();
}
示例11: RenderActionResultToString
/// <summary>
/// Renders an action result to a string. This is done by creating a fake http context
/// and response objects and have that response send the data to a string builder
/// instead of the browser.
/// </summary>
/// <param name="result">The action result to be rendered to string.</param>
/// <returns>The data rendered by the given action result.</returns>
protected string RenderActionResultToString(ActionResult result)
{
// Create memory writer.
var sb = new StringBuilder();
var memWriter = new StringWriter(sb);
// Create fake http context to render the view.
var fakeResponse = new HttpResponse(memWriter);
var fakeContext = new HttpContext(System.Web.HttpContext.Current.Request, fakeResponse);
var fakeControllerContext = new ControllerContext(
new HttpContextWrapper(fakeContext),
this.ControllerContext.RouteData,
this.ControllerContext.Controller);
var oldContext = System.Web.HttpContext.Current;
System.Web.HttpContext.Current = fakeContext;
// Render the view.
result.ExecuteResult(fakeControllerContext);
// Restore data.
System.Web.HttpContext.Current = oldContext;
// Flush memory and return output.
memWriter.Flush();
return sb.ToString();
}
示例12: createContent
private void createContent(String varName, Object objectToPass, StringReader reader, String outputFileName)
{
Dictionary<String, Object> map = new Dictionary<String, Object>();
map.Add(varName, objectToPass);
String outputContent = null;
StringTemplateGroup group = new StringTemplateGroup(reader);
var contentTemplate = group.GetInstanceOf("Content");
contentTemplate.Attributes = map;
outputContent = contentTemplate.ToString();
//StringBuilder sb = new StringBuilder(outputContent);
StringWriter writer = new StringWriter(new StringBuilder(outputContent));
writer.Flush();
StreamWriter fileWriter = null;
try
{
fileWriter = new StreamWriter(outputFileName + "/report.html.data");
fileWriter.Write(writer.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
writer.Close();
fileWriter.Close();
}
示例13: PrintToString
public static string PrintToString(this ImmutableEnvelope envelope, Func<object, string> serializer)
{
using (var writer = new StringWriter(CultureInfo.InvariantCulture))
{
writer.WriteLine("{0,12}: {1}", "EnvelopeId", envelope.EnvelopeId);
foreach (var attribute in envelope.Attributes)
{
writer.WriteLine("{0,12}: {1}", attribute.Key, attribute.Value);
}
writer.WriteLine(envelope.Message.GetType().Name);
try
{
var buffer = serializer(envelope.Message);
writer.WriteLine(buffer);
}
catch (Exception ex)
{
writer.WriteLine("Rendering failure");
writer.WriteLine(ex);
}
writer.WriteLine();
writer.Flush();
return writer.GetStringBuilder().ToString();
}
}
示例14: RenderActionToString
public static string RenderActionToString(this HtmlHelper helper, HttpRequest request, string controller, string action)
{
//Create memory writer
var sb = new StringBuilder();
var memWriter = new StringWriter(sb);
//Create fake http context to render the view
var fakeResponse = new HttpResponse(memWriter);
var fakeContext = new HttpContext(request, fakeResponse);
var fakeControllerContext = new ControllerContext(
new HttpContextWrapper(fakeContext),
helper.ViewContext.RouteData,
helper.ViewContext.Controller);
var oldContext = HttpContext.Current;
HttpContext.Current = fakeContext;
//Use HtmlHelper to render partial view to fake context
var html = new HtmlHelper(new ViewContext(fakeControllerContext,
new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), memWriter),
new ViewPage());
html.RenderAction(action, controller);
//Restore context
HttpContext.Current = oldContext;
//Flush memory and return output
memWriter.Flush();
return sb.ToString();
}
示例15: Minify
/// <summary>
/// Removes a comments and unnecessary whitespace from JavaScript code
/// </summary>
/// <param name="content">JavaScript content</param>
/// <returns>Minified JavaScript content</returns>
public String Minify(string content)
{
string minifiedContent;
lock (_minificationSynchronizer)
{
_theA = 0;
_theB = 0;
_theLookahead = EOF;
_theX = EOF;
_theY = EOF;
using (_reader = new StringReader(content))
using (_writer = new StringWriter())
{
InnerMinify();
_writer.Flush();
minifiedContent = _writer.ToString().TrimStart();
}
_reader = null;
_writer = null;
}
return minifiedContent;
}