gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:46:46 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/1984681/

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