【问题标题】:XML Serialize large collections of objectsXML 序列化大型对象集合
【发布时间】:2009-08-20 11:40:39
【问题描述】:

序列化大量对象的最佳方法是什么?我正在尝试针对模式序列化和验证 C# 中大约 70,000 个项目的大型项目集合。

未创建 XML 文件。我已经尝试了 1,000 个项目,它适用于更少的项目。

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
     }
}

【问题讨论】:

  • 结果如何?快吗?慢?
  • 好的,谢谢,这是其中的一部分。现在您必须向我们展示您用于序列化和验证的代码。

标签: c# xml serialization


【解决方案1】:

我们对我们正在做的一些工作进行了一些基准测试,发现 XmlSerialiser 比使用 XmlReader 处理大型 XML 文件更快,但对于小型文件则不然。我怀疑 XmlValidatingReader 可能会很快。不过,您可能需要对数据样本进行自己的基准测试。

我在网上某处找到了这段代码,用于针对 Xml 片段验证 xsd,稍加调整它也可能对您有用。

 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);
    }

【讨论】:

  • 感谢大卫的回答,我稍后会尝试,我目前将生成/写入 XML 文件作为我的首要任务,因为需要将这些文件取出。我可以选择不验证是否太麻烦,尽管我认为这样做更好。根据我的研究,似乎 XmlTextWriter 是更好的选择,但到目前为止我看到的所有示例代码似乎都是手动编写每个元素和属性。这是使用此类的唯一方法吗
猜你喜欢
  • 1970-01-01
  • 2012-01-11
  • 2013-09-18
  • 2010-11-30
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多