在Windows Mobile 6.1中序列化

时间:2012-12-28 16:05:34

标签: c# windows-mobile

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

使用.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>"

2 个答案:

答案 0 :(得分:1)

XmlSerializer相对非常慢。我建议考虑直接使用XmlTextWriter序列化你的对象。

代码会更复杂,但会更快,并且需要更少的内存 - 这两个因素通常在移动设备上非常强大。

答案 1 :(得分: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();
    }
  }
}

希望这会让你到达某个地方。不过,我甚至不确定这是不是你想要的。