views:

431

answers:

1

What is the best way to serialize a large collection of objects? I am attempting to serialize and validate against a schema a large collection of items about 70,000 items in c#.

XML file is not created. I have tried with a 1,000 items, it works fine with less items.

public void SerializeObject( Ojbect MyObj)
{
  XmlSerializer serializer = new XmlSerializer(MyObj.GetType());

  StreamWriter sw = new StreamWriter(“c:\file.xml”);
  serializer.Serialize(streamWriter, myObj);
  sw.Flush();
  sw.Close();
}

public void Validate()
{
     XmlSchema xmlSchema = “c:\myschema.xsd”
     XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
     xmlReaderSettings.ValidationType = ValidationType.Schema;
     xmlReaderSettings.Schemas.Add(xmlSchema);

     XmlReader xmlReader = XmlReader.Create(xmlStream, xmlReaderSettings);
     while (xmlReader.Read())
     {
         //do some stuff
     }
}
+1  A: 

We did some benchmarking for some work we were doing and found that the XmlSerialiser worked faster than using the XmlReader for large XML files, but not for small ones. I would suspect that the XmlValidatingReader would probably be quite quick. You may need to do your own benchmarking with samples of your data though.

I found this code online somewhere to do validation of xsd against an Xml fragment, with a few tweaks it may work for you too.

 public static bool Validate(string xsd, string xmlFrag)
    {
        if (string.IsNullOrEmpty(xmlFrag))
            return false;

        Trace.Indent();

        XmlValidatingReader reader = null;
        XmlSchemaCollection myschema = new XmlSchemaCollection();
        ValidationEventHandler eventHandler = new ValidationEventHandler(ShowCompileErrors);

        try
        {
            //Create the XmlParserContext.
            XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

            //Implement the reader. 
            reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
            //Add the schema.
            myschema.Add("", xsd);

            //Set the schema type and add the schema to the reader.
            reader.ValidationType = ValidationType.Schema;
            reader.Schemas.Add(myschema);

            while (reader.Read())
            {
            }

            Trace.WriteLine("Completed validating xmlfragment");
            return true;
        }
        catch (XmlException XmlExp)
        {
            Trace.WriteLine(XmlExp.Message);
        }
        catch (XmlSchemaException XmlSchExp)
        {
            Trace.WriteLine(XmlSchExp.Message);
        }
        catch (Exception GenExp)
        {
            Trace.WriteLine(GenExp.Message);
        }
        finally
        {
            Trace.Unindent();
        }
        return false;

    }
    public static void ShowCompileErrors(object sender, ValidationEventArgs args)
    {
        Trace.WriteLine("Validation Error: {0}", args.Message);
    }
David McEwing