Groups | Blog | Home
all groups > dotnet xml > april 2005 >

dotnet xml : Problem with XMLTextReader


Chris Lovett
4/19/2005 11:25:53 PM
Yes, it turns out that spaces before the xml declaration is not fully
standards compliant, so you XML would not have worked across other platforms
and we opted to go for best interoperability in .NET classes by enforcing
standards compliance (since interoperability is what XML is all about).

You could preload the file into a string and Trim the spaces then pass the
result via StringReader to XmlReader or you could provide a more efficient
solution by wrapping FileStream with your own clever Stream class that
strips initial whitespace.

[quoted text, click to view]

prasad
4/20/2005 12:00:00 AM
Hi,
I am using XMLTextReader class to read the xml files.
In some cases xml declaration tag might start after space/tab charecters.
These kind of files are supported by the browsers and xml dom's
but the xmltextreader's are not supporting to read this kind of files.

Using XMLTextReaders is there anyway to read this kind of files.

Thanks in advance,
Prasad Dannani.


prasad
4/20/2005 7:30:47 PM
In mycase, i have to use XMLTextReader because of its advantages. But if i
use filestreams and load the complete info to a string and then using
xmlreader classes to read the values then its a double work with out any
advantage.

Anyway thanks for helping. can anyone please suggest me that is there any
property or class to help me which will support all the advantages of
xmltextreaders


[quoted text, click to view]

Chris Lovett
4/21/2005 12:24:17 AM
There is no way to tell XmlTextReader to ignore the whitespace.

My suggestion of passing a wrapper Stream to XmlTextReader does require
double copy of read buffers, but other than it is pretty efficient.

new XmlTextReader(new WhiteSpaceTrimmingStream(new FileStream(filename)));

Where

class WhiteSpaceTrimmingReader : TextReader {
StreamReader file;
bool firstRead = true;

WhiteSpaceTrimmingStream(StreamReader file) {
this.file = file;
}

public override int Read(char[] buffer, int offset, int len) {
int found = 0;
while (found == 0) {
found = file.Read(buffer, offset, len);
if (firstRead) {
int i = 0;
while (i < found) {
char ch = buffer[i];
if (ch != ' ' && ch != '\t' && ch != '\r' && ch != '\n')
{
break;
}
}
if (i > 0) {
Array.Copy(buffer, i, buffer, 0, found - i);
found -= i;
}
}
}
return found;
}

// and delegate the rest of Stream API to underlying stream.
}


[quoted text, click to view]

AddThis Social Bookmark Button