Astronaut
Astronaut

Reputation: 7051

Serialize in Windows Mobile 6.1

I have an XML document that I am serializing, however it is taking a very long time. Is there a limitation on Windows Mobile 6.1 Pro that causes a serialization to be very slow? I am getting times in the 1-1.5 (s) for a 16Kb String.

Using .NET CF 3.5 Serializing into 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>"

Upvotes: 1

Views: 271

Answers (2)

user153923
user153923

Reputation:

OK, here's what I see you have:

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

I really don't understand how you are getting the string above into an XML document. However, like Ran said, you should look into a XmlTextWriter.

Using your string data, I'd write something like this (untested):

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

Hopefully, this will get you somewhere. I'm not even sure if that's what you are looking for, though.

Upvotes: 0

Ran
Ran

Reputation: 6159

XmlSerializer is relatively very slow. I would suggest considering using an XmlTextWriter directly to serialize your object.

The code would be more complicated but it will be much more faster and will require much less memory - two considerations that are usually very strong on a mobile device.

Upvotes: 1

Related Questions