阅读,编写xml和编码问题 [英] reading, writing xml and encoding question

查看:56
本文介绍了阅读,编写xml和编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究即将推出的c#.net 2.0项目,可能需要

读取和编写xml文件。我不想使用xmltextreader /

xmltextwriter,因为我更喜欢更低级别的文件访问权限和更多的b $ b控制,以及更小的内存占用,而且我不需要他们的大多数

功能。


我需要处理utf8和utf16编码,我需要插入

一些标签。


我应该使用streamreader / streamwriter类和编码

属性,还是有更适合的其他I / O类?

我应该读写字节以避免整个编码问题吗?


Bill

I am researching for an upcoming c# .net 2.0 project that may require
reading and writing xml files. I don''t want to use xmltextreader/
xmltextwriter as I prefer to have lower level file access and more
control, and a smaller memory footprint, and I wont need most of their
functionality.

I do need to handle utf8 and utf16 encodings and I need to insert
some tags.

Should I use the streamreader/streamwriter classes and the encoding
property, or are there other I/O classes better suited for this?
Should I read and write bytes to avoid the whole encoding issue?

Bill

推荐答案

4月30日下午3:01,billahi ... @ yahoo.com写道:
On Apr 30, 3:01 pm, billsahi...@yahoo.com wrote:

我正在研究即将推出的可能需要的c#.net 2.0项目

读写xml文件。我不想使用xmltextreader /

xmltextwriter,因为我更喜欢更低级别的文件访问权限和更多的b $ b控制,以及更小的内存占用,而且我不需要他们的大多数

功能。
I am researching for an upcoming c# .net 2.0 project that may require
reading and writing xml files. I don''t want to use xmltextreader/
xmltextwriter as I prefer to have lower level file access and more
control, and a smaller memory footprint, and I wont need most of their
functionality.



你有任何证据表明避免使用XmlTextReader /

XmlTextWriter实际上会给你一个明显更小的内存

足迹?你需要什么文件访问这些课程不会给你?
你呢?您是否可以通过向XmlTextReader / XmlTextWriter提供自己的流来实现相同级别的访问?


在较低级别获取XML内容可以是棘手的业务。

你确定你的时间最好是通过重新发明这个特定的轮子而不是专注于实际商业价值吗?


如果你对性能等有真正的顾虑,它绝对值得

*测量*可用的不同选项 - 看看是否有

如果你使用它实际上是一个问题假设有(并且你自己可以做得更好)内置的课程,而不是




Jon

Do you have any evidence to suggest that avoiding using XmlTextReader/
XmlTextWriter will actually give you a significantly smaller memory
footprint? What file access do you need that these classes won''t give
you? Could you possibly achieve the same level of access by providing
your own stream to XmlTextReader/XmlTextWriter?

Getting XML stuff right at a low level can be a tricky business. Are
you sure your time is best spent by reinventing this particular wheel
instead of concentrating on actual business value?

If you have real concerns about performance etc, it''s definitely worth
*measuring* the different options available - see whether there''s
actually a problem if you use the built-in classes, rather than
assuming there is (and that you could do better yourself).

Jon


如果你对性能等有真正的担忧,它绝对值得
If you have real concerns about performance etc, it''s definitely worth

*测量*可用的不同选项 - 看看是否有'如果你使用内置类,而不是假设有(并且你自己可以做得更好)
,那么
实际上是一个问题。
*measuring* the different options available - see whether there''s
actually a problem if you use the built-in classes, rather than
assuming there is (and that you could do better yourself).



把它放到上下文中 - 这是一个使用XmlWriter的粗略测试

写一个包含所有编码成本等的大文件,然后使用Stream

来编写原始二进制文件(重复的简单数据,中等长度 - 是的

我也尝试了缓冲流)。


结果表明没有真正的好处(事实上,许多测试使用XmlWriter显着更快地使用
) - 几乎可以肯定,因为CPU可以运行环硬盘。那么:你真的想在这里添加

复杂度(和bug)的层次吗?我不会......


c:\ xml.xml:5598ms,34MB,6.07MB / s

c:\ bin.bin: 4941ms,34MB,6.88MB / s

使用System.IO;

使用System.Xml;

使用System.Diagnostics ;

使用系统;


类程序

{

static void Main()

{

const string XML_PATH = @" c:\ xml.xml",BIN_PATH = @" c:\bin.bin" ;;


if(File.Exists(XML_PATH))File.Delete(XML_PATH);

秒表watch = Stopwatch.StartNew();

使用( XmlWriter writer = XmlWriter.Create(XML_PATH))

{

writer.WriteStartElement(" Foo");

for(int i = 0; i< 1500000; i ++)

{

writer.WriteStartElement(" Bar");

writer.WriteAttributeString(" val",i.ToString());

}

writer.WriteEndElement();

writer.Close();

}

watch.Stop();

long bytes = WriteWatch(watch.ElapsedMilliseconds,XML_PATH);


if(File.Exists(BIN_PATH))File.Delete(BIN_PATH);

watch = Stopwatch.StartNew();

//注意:这里也试过BufferedStream ...

using(Stream stream = File.Create(BIN_PATH) ))

{//将任何旧垃圾写入相同的文件大小

byte [] buffer = new byte [200];

while (字节0)

{

stream.Write(buffer,0,buffer.Length);

bytes - = buffer.Length; < br $>
}

stream.Close();

}

watch.Stop();

WriteWatch(watch.ElapsedMilliseconds,BIN_PATH);

}

静态长WriteWatch(长ms,字符串路径)

{

long bytes = new FileInfo(path).Length,mb = bytes /(1024 * 1024);

Console.WriteLine(" {0}:{1} ms, {2} MB,{3:###。##} MB / s",

路径,ms,mb,

mb /(ms / 1000M)) ;

返回字节;

}

}

To put that into context - here is a crude test that uses XmlWriter to
write a large file with all the cost of encoding etc, then uses a Stream
to write raw binary (of repeated simple data, moderate length - and yes
I tried the buffered stream too).

The results suggest no real benefit (in fact, many tests were
significantly quicker with XmlWriter) - almost certainly because the CPU
can run rings around the HDD. So: do you really want to add layers of
complexity (and bugs) here? I wouldn''t...

c:\xml.xml: 5598ms, 34MB, 6.07MB/s
c:\bin.bin: 4941ms, 34MB, 6.88MB/s

using System.IO;
using System.Xml;
using System.Diagnostics;
using System;

class Program
{
static void Main()
{
const string XML_PATH = @"c:\xml.xml", BIN_PATH = @"c:\bin.bin";

if (File.Exists(XML_PATH)) File.Delete(XML_PATH);
Stopwatch watch = Stopwatch.StartNew();
using (XmlWriter writer = XmlWriter.Create(XML_PATH))
{
writer.WriteStartElement("Foo");
for (int i = 0; i < 1500000; i++)
{
writer.WriteStartElement("Bar");
writer.WriteAttributeString("val", i.ToString());
}
writer.WriteEndElement();
writer.Close();
}
watch.Stop();
long bytes = WriteWatch(watch.ElapsedMilliseconds, XML_PATH);

if (File.Exists(BIN_PATH)) File.Delete(BIN_PATH);
watch = Stopwatch.StartNew();
// note: also tried BufferedStream here...
using (Stream stream = File.Create(BIN_PATH))
{ // write any old garbage to the same file size
byte[] buffer = new byte[200];
while (bytes 0)
{
stream.Write(buffer, 0, buffer.Length);
bytes -= buffer.Length;
}
stream.Close();
}
watch.Stop();
WriteWatch(watch.ElapsedMilliseconds, BIN_PATH);
}
static long WriteWatch(long ms, string path)
{
long bytes = new FileInfo(path).Length, mb = bytes / (1024 * 1024);
Console.WriteLine("{0}: {1}ms, {2}MB, {3:###.##}MB/s",
path, ms, mb,
mb / (ms / 1000M));
return bytes;
}
}


4月30日,8:53 * am,Jon Skeet [C#MVP] < sk ... @ pobox.comwrote:
On Apr 30, 8:53*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:

4月30日下午3:01,billahi ... @ yahoo.com写道:

你有没有证据表明避免使用XmlTextReader /

XmlTextWriter实际上会给你一个小得多的内存

足迹?
On Apr 30, 3:01 pm, billsahi...@yahoo.com wrote:
Do you have any evidence to suggest that avoiding using XmlTextReader/
XmlTextWriter will actually give you a significantly smaller memory
footprint?



Jon,


是的。广泛地测试了它们。读者慢慢爬行,非常大的文件和内存使用量猛增(超过500Mb,

取决于xml结构和机器)。我可以使用streamreader阅读几乎任何

大小的文件。如果我能找到一个xml编辑器而不是
来处理这些文件,并检查结构良好,我可以用它来完成一些

的工作。


Bill

Jon,

Yes. tested them extensively. the reader slows to a crawl with very
large files and memory usage skyrockets(somewhere over 500Mb,
depending on xml structure and machine). I can read pretty much any
size file with streamreader. If I could find an xml editor than can
handle such files, and checks well-formedness, I could use it for some
of the work.

Bill


这篇关于阅读,编写xml和编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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