Share on Facebook
Welcome to MSDN Blogs Sign in | Join | Help

Reading Fragments with XmlTextReader

Daniel points out a way to read fragments with XmlTextReader, and mentions that I posed the problem.

I was working with some rather nasty “XML” that was not well-formed, and had to find a way to successfully parse it.  The SgmlReader proved perfect for this, save for 1 outstanding problem:  the XML did not contain a root node.  SgmlReader will read the first node in a document until it encounters the closing element for the first node encountered.  When this happens, it sets its internal state to EOF (End of File).

Using Daniel's approach, I could “trick” the SgmlReader to thinking that there was a root node for the document, and then parse the entire document as though it were well-formed.

FileStream fs = new XmlFragmentStream(“someBIGfile.xml“);
    
SgmlReader reader = new Sgml.SgmlReader();  
      
reader.InputStream = new StreamReader(fs,System.Text.Encoding.UTF8);
reader.WhitespaceHandling = System.Xml.WhitespaceHandling.None;
object docNode = reader.NameTable.Add("DOCUMENT");

while(reader.Read() )
{    
 //Use the NameTable for reference comparison                    
 if(reader.NodeType == XmlNodeType.Element)
 {
  if(docNode == reader.Name)
  {       
   writer.WriteNode(reader);
  }
 }     
}

Published Saturday, April 24, 2004 1:05 PM by kaevans
Filed under: ,

Comments

# re: Reading Fragments with XmlTextReader

Saturday, April 24, 2004 3:10 PM by Daniel Cazzulino
Good to know the complete scenario. Now it makes even more sense the approach at the stream level as compared to what Oleg suggested about using the special XmlTextReader constructor taking the XmlParsingContext stuff...

# Reading XML fragments with XmlTextReader

Saturday, April 24, 2004 7:22 PM by kzu.net

# Take Outs for 26 April 2004

Tuesday, April 27, 2004 12:29 AM by Enjoy Every Sandwich
Take Outs for 26 April 2004
New Comments to this post are disabled
 
Page view tracker