XElement.ReadFrom skips nodes depending on whitespace

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im using the technique described in http://msdn.microsoft.com/en-us/library/bb387008%28v=vs.90%29.aspx
How to: Stream XML Fragments with Access to Header Information to use LINQ with large XML documents with .NET 3.5.
If I delete white space between nodes, only every other node is returned. Here is a simplified test case that illustrates the problem:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Xml.Linq;
<span style="color:Blue; using System.Xml;
<span style="color:Blue; using System.IO;

<span style="color:Blue; namespace LinqDefect
{
<span style="color:Blue; class Program
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
<span style="color:Blue; string goodInput =
<span style="color:#A31515; @"<root>
<a>1
<a>2
<a>3
<a>4
</root>";
<span style="color:Blue; string badInput = <span style="color:#A31515; @"<root><a>1 <a>2 <a>3 <a>4 </root>";

File.WriteAllText(<span style="color:#A31515; "good.xml", goodInput);
File.WriteAllText(<span style="color:#A31515; "bad.xml", badInput);

Console.WriteLine(<span style="color:#A31515; "using good input:");
<span style="color:Blue; foreach (<span style="color:Blue; var a <span style="color:Blue; in StreamElements(<span style="color:#A31515; "good.xml", <span style="color:#A31515; "a"))
Console.WriteLine(a.Value);

Console.WriteLine(<span style="color:#A31515; "nusing bad input:");
<span style="color:Blue; foreach (<span style="color:Blue; var a <span style="color:Blue; in StreamElements(<span style="color:#A31515; "bad.xml", <span style="color:#A31515; "a"))
Console.WriteLine(a.Value);

File.Delete(<span style="color:#A31515; "good.xml");
File.Delete(<span style="color:#A31515; "bad.xml");
}

<span style="color:Blue; public <span style="color:Blue; static IEnumerable<XElement> StreamElements(<span style="color:Blue; string fileName, XName name)
{
<span style="color:Blue; using (XmlReader reader = XmlReader.Create(fileName))
{
reader.MoveToContent();
XElement node = <span style="color:Blue; null;

<span style="color:Blue; while (reader.Read())
{
<span style="color:Blue; if (reader.NodeType == XmlNodeType.Element &&
reader.Name == name.LocalName)
{
node = XElement.ReadFrom(reader) <span style="color:Blue; as XElement;
<span style="color:Blue; if (node != <span style="color:Blue; null)
yield <span style="color:Blue; return node;
}
}
}
}
}
}
[/code]

Here is the output when this is run:

<pre>using good input:
1
2
3
4

using bad input:
1
3
Press any key to continue . . .[/code]

They should match -- the only difference between goodInput and badInput is that I removed the newlines between the <a> nodes.
Im pretty sure Im doing things correctly. So, assuming this is a bug, does anyone have either a fix or a good workaround? Ive tried, during creation of my XML files, to use
http://msdn.microsoft.com/en-us/library/w703ce6s%28v=VS.90%29.aspx XmlTextWriter and
http://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.writewhitespace%28v=VS.90%29.aspx
WriteWhitespace to put the newline characters in between these nodes, but it doesnt seem to work.

View the full article
 
Back
Top