Gokul's Blog

Working with Large Xml files – part 2

Leave a comment

http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx

for writing large xml files:   http://www.codeproject.com/KB/XML/XStreamingElement_Merge.aspx?display=Mobile

private static IEnumerable<XElement> StreamElements(
  string fileName,
  string elementName)
{
  using (var rdr = XmlReader.Create(fileName))
  {
	rdr.MoveToContent();
	while (rdr.Read())
	{
	  if ((rdr.NodeType == XmlNodeType.Element) && (rdr.Name == elementName))
	  {
		var e = XElement.ReadFrom(rdr) as XElement;
		yield return e;
	  }
	}
	rdr.Close();
  }

XMLReader Implementation: http://msdn.microsoft.com/en-us/library/ff647804.aspx

while (reader.Read())
{
    switch (reader.NodeType)
    {
      case System.Xml.XmlNodeType.Element :
      {
        if( reader.Name.Equals("patient")
        && reader.GetAttribute("number").Equals("25") )
      {
        doc = new System.Xml.XmlDocument();
        XmlNode node = doc.ReadNode( reader );
        doc.AppendChild( node );
      }
      break;
    }
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s