DataContractSerializer如果有异常,我正在使用序列化EF4对象到xml.在我的调试日志中,我可以看到当出现问题时想要的是数据内容.
我有两个版本的代码:一个版本序列化为一个文件,一个版本序列化为一个字符串使用StringWriter.
将大项目序列化到文件时,我获得大约16kb的有效xml.当序列化相同的项目到字符串时,xml在12kb之后被截断.知道导致截断的原因吗?
...
var entity = ....
SaveAsXml(entity, @"c:\temp\EntityContent.xml"); // ok size about 16100 btes
var xmlString = GetAsXml(entity); // not ok, size about 12200 bytes
// to make shure that it is not Debug.Writeline that causes the truncation
// start writing near the end of the string
// only 52 bytes are written although the file is 16101 bytes long
System.Diagnostics.Debug.Writeline(xml.Substring(12200));
Run Code Online (Sandbox Code Playgroud)
我的字符串被截断的任何想法?
这是序列化到文件的代码,可以正常工作
public static void SaveAsXml(object objectToSave, string filenameWithPath)
{
string directory = Path.GetDirectoryName(filenameWithPath); …Run Code Online (Sandbox Code Playgroud) 我编写了一个接受a TextWriter作为参数的方法(通常是Console.Out,但不一定).
当我调用此方法时,会向TextWriter写入一些进度信息.但是,由于此方法可能会运行很长时间,我想用一些状态信息更新我的UI.
目前,我正在使用StringWriter但它没有任何事件.所以第一次从StringWriter方法完成后得到结果.
现在我正在搜索一个继承自TextWriter并触发TextChanged事件或类似事件的类.我知道这不应该是难以实现的,但我敢打赌CLR团队已经为我做了,我找不到合适的课程.
我正在使用一个StringWriter我传递给foreach循环中写入值的方法.我相信这导致产生两个警告:
CA2000:Microsoft.Reliability:在方法'ToCsvService.ToCsv()'中,对象'sw'不是沿所有异常路径放置的.在对所有引用超出范围之前,调用System.IDisposable.Dispose对象'sw'.
和
CA2202:Microsoft.Usage:对象'sw'可以在方法'ToCsvService.ToCsv()'中多次处理.为避免生成System.ObjectDisposedException,不应在对象上多次调用Dispose.
public string ToCsv()
{
IEnumerable<string> props = GetProperties();
StringWriter sw = new StringWriter(); // first warning here
sw.WriteLine(GetHeadings(props));
WriteValues(props, sw);
sw.Close();
string returnCsv = sw.ToString();
sw.Dispose(); // second warning here
return returnCsv;
}
Run Code Online (Sandbox Code Playgroud)
我GetProperties()从被称为方法的列表中省略了,因为它似乎并不相关.
private string GetHeadings(IEnumerable<string> props)
{
string headings = String.Join(",",
props.Select(prop =>
_headings.ContainsKey(prop) ? _headings[prop] : prop));
return headings;
}
private void WriteValues(IEnumerable<string> props, StringWriter sw)
{
foreach (object obj in _collection)
{
var x = obj.GetType().GetProperties() …Run Code Online (Sandbox Code Playgroud) UnZipFile方法从写入数据inputStream到outputWriter.为什么要sr.ToString()返回System.Byte[]而不是数据?
using (var sr = new StringWriter())
{
UnZipFile(response.GetResponseStream(), sr);
var content = sr.ToString();
}
public static void UnZipFile(Stream inputStream, TextWriter outputWriter)
{
using (var zipStream = new ZipInputStream(inputStream))
{
ZipEntry currentEntry;
if ((currentEntry = zipStream.GetNextEntry()) != null)
{
var size = 2048;
var data = new byte[size];
while (true)
{
size = zipStream.Read(data, 0, size);
if (size > 0)
{
outputWriter.Write(data);
}
else
{
break;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想要像 xml
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schema.test.com/test">
<test1>1</test1>
.
.
.
.
<test9>9</test9>
</root>
Run Code Online (Sandbox Code Playgroud)
所以我使用以下代码
Dictionary<string, object> Data = new Dictionary<string, object>();
for (int i = 0; i < 10; i++)
{
Data.Add("Test" + i, i);
}
Console.WriteLine(DateTime.Now.ToShortTimeString());
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace xsd = XNamespace.Get("http://www.w3.org/2001/XMLSchema");
XNamespace xsd1 = XNamespace.Get("http://schema.test.com/test");
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-16", "yes"),
new XElement(
"root",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(XNamespace.Xmlns + "xsd", xsd),
new XAttribute(XNamespace.None + "xmlns", xsd1),
from p …Run Code Online (Sandbox Code Playgroud) 我有 100 个文本框,我试图将这些文本框中的所有文本写入文本文件,这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 9; i++)
{
for (int j = 0; j <= 9; j++)
{
TextBox tb = new TextBox();
tb.MaxLength = (1);
tb.Width = Unit.Pixel(40);
tb.Height = Unit.Pixel(40);
// giving each textbox a different id 00-99
tb.ID = i.ToString() + j.ToString();
Panel1.Controls.Add(tb);
}
Literal lc = new Literal();
lc.Text = "<br />";
Panel1.Controls.Add(lc);
}
}
protected void btnShow_Click(object sender, EventArgs e)
{
StringWriter stringWriter …Run Code Online (Sandbox Code Playgroud) 我在其他语言中发现了这个问题,但还没有在java应用程序中找到解决这个问题的方法.
我有一个.txt包含数百万条记录的大文件.每条记录都是/n分隔的.基本上它是来自表的单列数据.目标是从输入文件中读取数据并对其进行分区.然后将分区数据写入新文件.例如,一个包含200万条记录的文件将成为200个文件,每个文件有10,000条记录(最后一个文件包含<10,000个.)
我成功地读取和分区数据.我成功创建了第一个文件,并且正确命名.
问题是只创建了一个文件,它是空的.代码编译和运行没有错误或异常.
我的代码如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
public class ChunkTextFile {
private static final String inputFilename = "inputFile.txt";
public static void main(String[] args) {
BufferedReader reader = null;
BufferedWriter fileWriter = null;
BufferedWriter lineWriter = null;
StringWriter stringWriter = null;
// Create an ArrayList object to hold the lines of input file
List<String> lines = new …Run Code Online (Sandbox Code Playgroud) stringwriter ×7
c# ×4
.net ×2
c#-4.0 ×1
collections ×1
events ×1
java ×1
java-8 ×1
linq-to-xml ×1
stream ×1
text-files ×1
textwriter ×1