2

我正在使用一个复杂的 xml 模式,为此我使用 xsd.exe 创建了一个类结构(付出了一些努力)。我现在可以可靠地将 xml 反序列化为生成的类结构。例如,考虑 Web 服务中的以下 xml:

<ODM FileType="Snapshot" CreationDateTime="2009-10-09T19:58:46.5967434Z" ODMVersion="1.3.0" SourceSystem="XXX" SourceSystemVersion="999">
  <Study OID="2">
    <GlobalVariables>
      <StudyName>Test1</StudyName>
      <StudyDescription/>
      <ProtocolName>Test0001</ProtocolName>
    </GlobalVariables>
    <MetaDataVersion OID="1" Name="Base Version" Description=""/>
    <MetaDataVersion OID="2" Name="Test0001" Description=""/>
    <MetaDataVersion OID="3" Name="Test0002" Description=""/>
  </Study>
</ODM>

我可以按如下方式反序列化 xml:

public ODMcomplexTypeDefinitionStudy GetStudy(string studyId)
{
  ODMcomplexTypeDefinitionStudy study = null;
  ODM odm = Deserialize<ODM>(Service.GetStudy(studyId));
  if (odm.Study.Length > 0)
    study = odm.Study[0];
  return study;
}

Service.GetStudy() 从 Web 服务返回一个 HTTPResponse 流。而 Deserialize() 是一个辅助方法,将流反序列化为对象类型 T。

我的问题是:让反序列化过程创建整个类结构并反序列化 xml 是否更有效,或者仅获取感兴趣的 xml 并反序列化该 xml 是否更有效。例如,我可以将上面的代码替换为:

public ODMcomplexTypeDefinitionStudy GetStudy(string studyId)
{
  ODMcomplexTypeDefinitionStudy study = null;
  using (XmlReader reader = XmlReader.Create(Service.GetStudy(studyId)))
  {
    XDocument xdoc = XDocument.Load(reader);
    XNamespace odmns = xdoc.Root.Name.Namespace;
    XElement elStudy = xdoc.Root.Element(odmns + "Study");
    study = Deserialize<ODMcomplexTypeDefinitionStudy>(elStudy.ToString());
  }
return study;
}

I suspect that the first approach is preferred -- there is a lot of dom manipulation going on in the second example, and the deserialization process must have optimizations; however, what happens when the xml grows dramatically? Let's say the source returns 1 MB of xml and I'm really only interested in a very small component of that xml. Should I let the deserialzation process fill up the containing ODM class with all it's arrays and properties of child nodes? Or just go get the child node as in the second example!!??

Not sure this helps, but here's a summary image of the dilemma:

alt text

4

2 回答 2

1

Brett,

Later versions of .net will build custom serializer assemblies. Click on project properties -> build and look for "Generate serialization assemblies" and change to On. The XML deserializer will use these assemblies which are customized to the classes in your project. They are much faster and less resource intensive since reflection is not involved.

I would go this route so that if you class changes you will not have to worry about serialization issues. Performance should not be an issue.

于 2009-10-12T16:16:12.163 回答
0

I recommend that you not preoptimize. If you have your code working, then use it as it is. Go on to work on some code that is not finished, or which does not work.

Later, if you find you have a performance problem in that area, you can explore performance.

于 2009-10-12T16:09:05.047 回答