Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

c# - How to avoid memory exception when reading, creating and sending a very large xml file?

I am working on to optimize our code so that we can read, create and send xml file which can be very large in size (2GB).

For read and create we are using XmlReader class.

We actually get an XML string from some other service. If we store the XML string in a string variable it takes the same amount of memory. That point aside, please suggest the best way to deal with the XML string so the memory out of bound exception doesn't occur.

I can not show code over here due to company policies but that should not matter because code is already working but in case of large xml string its giving:

memory exception

...as mentioned.

EXPLANATION :

We get 2GB Xml from a service.

We process it using streaming.

Since we need to read that xml using xmlreader, we pass the xml in the form of string to create a new xml with almost same size (2GB)

byte[] msg = Buffer.ExtractMessage(messageStart, messageEnd);  
string msg1 = Encoding.UTF8.GetString(msg);

CreateNewXMLFileFromTheCurrentXmlString(msg1);

We then send that new xml to some other service.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The best way would be to use a well normalized and indexed database if that's possible for you. Then getting the data by using LINQ should solve your problems. And the problem is the source and not your logic as XML files shouldn't be as big as yours.

Take a look here: LINQ TO XML


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...