1

我有一个要序列化的 XML 文档,但是它需要很长时间。Windows Mobile 6.1 Pro 是否存在导致序列化速度非常慢的限制?对于 16Kb 字符串,我得到了 1-1.5 (s) 的时间。

使用 .NET CF 3.5 序列化到 RAM。

serializer = new XmlSerializer(typeof(Request_PrintInfo));

"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
        "<Request_TestSale xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
        "  <Product>"
        "    <Ref>1</Ref>"
        "    <Name>Product 1</Name>"
        "    <ShortName>P1</ShortName>"
        "    <Abbreviation>P.1</Abbreviation>"
        "    <Id>494a8011-16a0-46ff-980f</Id>"
        "    <Attribs>"
        "      <ConfigAttribute>"
        "        <Name>price</Name>"
        "        <Required>false</Required>"
        "        <ReadOnly>true</ReadOnly>"
        "        <Value>200</Value>"
        "      </ConfigAttribute>"
        "      <ConfigAttribute>"
        "        <Name>Quantity</Name>"
        "        <Required>true</Required>"
        "        <ReadOnly>true</ReadOnly>"
        "        <Value>1</Value>"
        "      </ConfigAttribute>"
        "    </Attribs>"
        "  </Product>"
        "</Request_TestSale>"
4

2 回答 2

1

XmlSerializer 相对来说很慢。我建议考虑直接使用 XmlTextWriter 来序列化您的对象。

代码会更复杂,但速度会更快,并且需要更少的内存——这两个考虑因素通常在移动设备上非常重要。

于 2012-12-28T17:11:48.590 回答
0

好的,这就是我看到的:

private void DoNotUnderstandThis() {
  var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Request_PrintInfo));
  string data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
     "<Request_TestSale xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
     "  <Product>" +
     "    <Ref>1</Ref>" +
     "    <Name>Product 1</Name>" +
     "    <ShortName>P1</ShortName>" +
     "    <Abbreviation>P.1</Abbreviation>" +
     "    <Id>494a8011-16a0-46ff-980f</Id>" +
     "    <Attribs>" +
     "      <ConfigAttribute>" +
     "        <Name>price</Name>" +
     "        <Required>false</Required>" +
     "        <ReadOnly>true</ReadOnly>" +
     "        <Value>200</Value>" +
     "      </ConfigAttribute>" +
     "      <ConfigAttribute>" +
     "        <Name>Quantity</Name>" +
     "        <Required>true</Required>" +
     "        <ReadOnly>true</ReadOnly>" +
     "        <Value>1</Value>" +
     "      </ConfigAttribute>" +
     "    </Attribs>" +
     "  </Product>" +
     "</Request_TestSale>";
}

我真的不明白您是如何将上面的字符串放入 XML 文档中的。但是,就像 Ran 所说,您应该查看XmlTextWriter.

使用你的字符串数据,我会写这样的东西(未经测试):

private const string CRLF = "\r\n";
private const string CRLFTAB = "\r\n\t";
private const string CRLFTABTAB = "\r\n\t\t";
private const string CRLFTABTABTAB = "\r\n\t\t";
private object writeLock = new object();

private void WriteToXml(string filename) {
  lock (writeLock) {
    using (FileStream stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.Read)) {
      using (XmlTextWriter xw = new XmlTextWriter(stream, Encoding.UTF8)) {
        xw.WriteStartElement("Product"); // writes the root
        {
          xw.WriteWhitespace(CRLFTAB);
          xw.WriteElementString("Ref", "1");
          xw.WriteWhitespace(CRLFTAB);
          xw.WriteElementString("Name", "Product 1");
          xw.WriteWhitespace(CRLFTAB);
          xw.WriteElementString("ShortName", "P1");
          xw.WriteWhitespace(CRLFTAB);
          xw.WriteElementString("Abbreviation", "P.1");
          xw.WriteWhitespace(CRLFTAB);
          xw.WriteElementString("Id", "494a8011-16a0-46ff-980f");
          xw.WriteWhitespace(CRLFTAB);
          xw.WriteStartElement("Attribs");
          {
            xw.WriteWhitespace(CRLFTABTAB);
            xw.WriteStartElement("ConfigAttribute");
            {
              xw.WriteWhitespace(CRLFTABTABTAB);
              xw.WriteElementString("Name", "price");
              xw.WriteWhitespace(CRLFTABTABTAB);
              xw.WriteElementString("Required", "false");
              xw.WriteWhitespace(CRLFTABTABTAB);
              xw.WriteElementString("ReadOnly", "true");
              xw.WriteWhitespace(CRLFTABTABTAB);
              xw.WriteElementString("Value", "200");
              xw.WriteWhitespace(CRLFTABTABTAB);
            }
            xw.WriteEndElement(); // Write the close tag for the ConfigAttribute element
            xw.WriteWhitespace(CRLFTABTAB);
            xw.WriteWhitespace(CRLFTABTAB);
            xw.WriteStartElement("ConfigAttribute");
            {
              xw.WriteWhitespace(CRLFTABTABTAB);
              xw.WriteElementString("Name", "Quantity");
              xw.WriteWhitespace(CRLFTABTABTAB);
              xw.WriteElementString("Required", "true");
              xw.WriteWhitespace(CRLFTABTABTAB);
              xw.WriteElementString("ReadOnly", "true");
              xw.WriteWhitespace(CRLFTABTABTAB);
              xw.WriteElementString("Value", "1");
              xw.WriteWhitespace(CRLFTABTABTAB);
            }
            xw.WriteEndElement(); // Write the close tag for the ConfigAttribute element
            xw.WriteWhitespace(CRLFTABTAB);
          }
          xw.WriteEndElement(); // Write the close tag for the Attribs element
          xw.WriteWhitespace(CRLF);
        }
        xw.WriteEndElement(); // Write the close tag for the root element
        xw.WriteWhitespace(CRLF);
        xw.Flush();
        xw.Close();
      }
      stream.Close();
    }
  }
}

希望这会让你到达某个地方。不过,我什至不确定这是否是您要寻找的东西。

于 2013-01-03T16:06:25.287 回答