gpt4 book ai didi

c# - 跟踪大型 XML 文件的加载进度

转载 作者:行者123 更新时间:2023-12-01 18:09:48 25 4
gpt4 key购买 nike

我尝试跟踪 dotnet(C#,框架 3.5 SP1)中大 XML 文件(我不是这些文件的提供者)的加载进度:通过网络文件共享从 1 MB 到 300 MB。

我使用 XmlReader 进行加载,而不是直接使用 XmlDocument.Load 方法来加速加载过程。

顺便说一句,我在互联网/文档上找不到有关如何跟踪此加载进度的信息:似乎不存在委托(delegate)/事件。有什么方法可以执行此任务吗?拥有用于 XML 保存目的的功能可能会很不错。

谢谢

最佳答案

假设您正在从流中读取内容,这是一个(不完美的)示例,说明如何执行此操作...基本上,ProgressStreamWrapper 会包装文件流,并在 Position 更改时引发一个事件。

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Reading big file...");

FileStream fileStream = File.OpenRead("c:\\temp\\bigfile.xml");
ProgressStreamWrapper progressStreamWrapper = new ProgressStreamWrapper(fileStream);
progressStreamWrapper.PositionChanged += (o, ea) => Console.WriteLine((double) progressStreamWrapper.Position / progressStreamWrapper.Length * 100 + "% complete");
XmlReader xmlReader = XmlReader.Create(progressStreamWrapper);

while (xmlReader.Read())
{
//read the xml document
}

Console.WriteLine("DONE");
Console.ReadLine();
}
}


public class ProgressStreamWrapper : Stream, IDisposable
{
public ProgressStreamWrapper(Stream innerStream)
{
InnerStream = innerStream;
}

public Stream InnerStream { get; private set; }

public override void Close()
{
InnerStream.Close();
}

void IDisposable.Dispose()
{
base.Dispose();
InnerStream.Dispose();
}

public override void Flush()
{
InnerStream.Flush();
}

public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
return InnerStream.BeginRead(buffer, offset, count, callback, state);
}

public override int EndRead(IAsyncResult asyncResult)
{
int endRead = InnerStream.EndRead(asyncResult);
OnPositionChanged();
return endRead;
}

public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
return InnerStream.BeginWrite(buffer, offset, count, callback, state);
}

public override void EndWrite(IAsyncResult asyncResult)
{
InnerStream.EndWrite(asyncResult);
OnPositionChanged(); ;
}

public override long Seek(long offset, SeekOrigin origin)
{
long seek = InnerStream.Seek(offset, origin);
OnPositionChanged();
return seek;
}

public override void SetLength(long value)
{
InnerStream.SetLength(value);
}

public override int Read(byte[] buffer, int offset, int count)
{
int read = InnerStream.Read(buffer, offset, count);
OnPositionChanged();
return read;
}

public override int ReadByte()
{
int readByte = InnerStream.ReadByte();
OnPositionChanged();
return readByte;
}

public override void Write(byte[] buffer, int offset, int count)
{
InnerStream.Write(buffer, offset, count);
OnPositionChanged();
}

public override void WriteByte(byte value)
{
InnerStream.WriteByte(value);
OnPositionChanged();
}

public override bool CanRead
{
get { return InnerStream.CanRead; }
}

public override bool CanSeek
{
get { return InnerStream.CanSeek; }
}

public override bool CanTimeout
{
get { return InnerStream.CanTimeout; }
}

public override bool CanWrite
{
get { return InnerStream.CanWrite; }
}

public override long Length
{
get { return InnerStream.Length; }
}

public override long Position
{
get { return InnerStream.Position; }
set
{
InnerStream.Position = value;
OnPositionChanged();
}
}

public event EventHandler PositionChanged;

protected virtual void OnPositionChanged()
{
if (PositionChanged != null)
{
PositionChanged(this, EventArgs.Empty);
}
}

public override int ReadTimeout
{
get { return InnerStream.ReadTimeout; }
set { InnerStream.ReadTimeout = value; }
}

public override int WriteTimeout
{
get { return InnerStream.WriteTimeout; }
set { InnerStream.WriteTimeout = value; }
}
}

关于c# - 跟踪大型 XML 文件的加载进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7207310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com