3

i have xml file 230 mb big with 230 million length when i try to import the file to my android using an app that we made but when attempting readtoend string into variable im getting an error :

System.outofmemoryexception : out of memory at (wrapper managed-to-native) system.string.fastallocatestring(int) at system.text.stringbuilder.tostring()

here's the following code

var documentsPath = Android.OS.Environment.ExternalStorageDirectory.ToString();
            var dirName = System.IO.Path.Combine(documentsPath.ToString(), "Stock");

            var filePath = System.IO.Path.Combine(dirName, "Data.xml");
            TextReader tr = new StreamReader(filePath);
            string result = tr.ReadToEnd();
            tr.Close();

            var st = new XStreamingElement(filePath);

            return result;

can anyone help, thanks in advance.

2

1 Answer 1

4

Try using XMLTextReader class instead as the example below:

XmlTextReader myTextReader = new XmlTextReader(filename);
myTextReader.WhitespaceHandling = WhitespaceHandling.None;
while (myTextReader.Read())
{
    if (myTextReader.NodeType == XmlNodeType.Element &&
        myTextReader.LocalName == "Reward" &&
        myTextReader.IsStartElement() == true)
        {
            ProcessRewardNode(myTextReader);
                myTextReader.Skip();
    }
}

Here is the method implementation of ProcessRewardNode:

private void ProcessRewardNode(XmlTextReader RewardReader)
{
    XmlDocument RewardXmlDoc = new XmlDocument();
    RewardXmlDoc.LoadXml(RewardReader.ReadOuterXml());
    // we can use xpath as below
    myID = RewardXmlDoc.SelectSingleNode("Reward/myID").InnerText;
}

for more info visit XMLTextReader's official documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.