0

Currently trying to make a method to read in XML files at the moment 50 lines at a time this will be increased to allow larger files to be used in the program.

At the moment i am trying to accomplish this with the following code.

 List<dataclass.DataRecord> list = new List<dataclass.DataRecord>();
 string filename = "FileLocation"
 XmlDocument testing = new XmlDocument();

 //using (StreamReader streamreader = new StreamReader(filename))
 using (XmlTextReader reader = new XmlTextReader(new StringReader(filename)))
 {
   while (reader.Read() != null)
   {
    for (int i = 0; i < 50; i++)
    {
     testing.Load(reader);

     //list.add(line);
     Console.WriteLine(testing);
     //testing.Load(reader);
     }
   }
 }

commented lines are just from previous ideas i used to accomplish my goal and the filename has been taken out as i just prefer not to place that online.

Basically at the moment i keep getting the following error:

Data at the root level is invalid. Line 1, position 1.

So i dunno if I am:

A. Going about this the right way.

B. Is the only way to fix this error is by surrounding the "testing.load" by "root + /root" tags

hope someone can help thank.

user2169674
  • 87
  • 2
  • 8
  • 1
    You should think of XML like a **Tree** rather than a **Flat file** with lines. A line doesn't make sense in XML, you could be cutting a **node** up if you only read 50 "lines". What are you achieving by doing this? – Belogix Apr 02 '13 at 11:10
  • okay ill give that a look over, I am rather new to doing this. – user2169674 Apr 02 '13 at 11:11
  • to answer you question in what im achieving, I am just trying to load the file gradually rather than all in one go . – user2169674 Apr 02 '13 at 12:36
  • OK, I've added as an answer as you're not supposed to use comments as a communication method and my idea is a bit longer than a comment. – Belogix Apr 02 '13 at 12:45

3 Answers3

0

If you are only trying to load xml into XmlDocument - why not just

 XmlDocument testing = new XmlDocument();
 testing.Load(filename);
evgenyl
  • 7,837
  • 2
  • 27
  • 32
  • I know i could just do that but, but the program maybe needed to load very large files, so this method of loading the file in gradually hopefully should be efficient – user2169674 Apr 02 '13 at 12:22
0

If your XML file is really big, you're better off using some sort of pull parser (parses tag-by-tag, attribute-by-attribute, etc) rather than DOM parser (loads whole document during parsing, keeps it in memory).

pfyod
  • 617
  • 5
  • 11
0

As I explained in my comment XML consists of nodes whereas you are looking at it as though it were a flat-file with lines.

There are a couple of Stackoverflow questions with answers that match what you are trying to do. The real question is "How can you load a large XML file". The answer is to use a stream rather than loading in one big chunk, following on from there you can find lots of resources about using XmlReader.

Couple of pointers to other SO articles:

C# and Reading Large XML Files

Reading large XML documents in .net

Hope that helps!

Community
  • 1
  • 1
Belogix
  • 8,129
  • 1
  • 27
  • 32
  • helps alot, just as you posted i was looking at these links as well from a combination of your comment and pyfods answers. – user2169674 Apr 02 '13 at 13:05