在Windows Mobile 6.1中序列化
本文关键字:序列化 Windows Mobile | 更新日期: 2023-09-27 17:49:54
我有一个要序列化的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>"
XmlSerializer相对很慢。我建议考虑直接使用xmltextwwriter来序列化你的对象。
代码会更复杂,但速度会更快,占用的内存也会更少——这两点在移动设备上通常是非常重要的。
好的,这是我看到的:
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();
}
}
}
希望这能让你有所收获。我甚至不确定这是否是你想要的