How to Serialize BsonValue by XmlSerializer (C#)

235 views
Skip to first unread message

胡 伟敏

unread,
Nov 30, 2011, 2:17:40 AM11/30/11
to mongodb-user
When I want to Serialize a BsonValue link BsonString with
XmlSerializer at C#, A exception is happen.
Who can give me some advice about how to Serialize BsonValue via
C#,Thank you.

Nat

unread,
Nov 30, 2011, 2:18:49 AM11/30/11
to mongod...@googlegroups.com
Can you post your code and stacktrace?
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

胡 伟敏

unread,
Nov 30, 2011, 2:25:48 AM11/30/11
to mongodb-user
[Serializable]
public class DataFilter
{
...
...
public struct QueryConditionInputItem
{
public string StartMark;
public string ColName;
public CompareEnum Comp;
public BsonValue Value;
public string EndMark;
}
...
...
public void SaveFilter(String FileName)
{
FileStream fs = null;
XmlSerializer xs = new XmlSerializer(typeof(DataFilter));
fs = new FileStream(FileName, FileMode.Create,
FileAccess.Write);
xs.Serialize(fs, this);
fs.Close();
}
}

When I want to SaveFilter.....


{"MongoDB.Bson.BsonString cannot be serialized because it does not
have a parameterless constructor."}

at System.Xml.Serialization.TypeDesc.CheckSupported()
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type,
MemberInfo source, Boolean directReference, Boolean throwOnError)
at
System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type
type)
at
System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Object
o)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterDataFilter.Write4_BsonValue(String
n, String ns, BsonValue o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterDataFilter.Write5_QueryConditionInputItem(String
n, String ns, QueryConditionInputItem o, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterDataFilter.Write8_DataFilter(String
n, String ns, DataFilter o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterDataFilter.Write9_DataFilter(Object
o)

Nat Luengnaruemitchai

unread,
Nov 30, 2011, 2:44:31 AM11/30/11
to mongod...@googlegroups.com
BsonValue object doesn't support XmlSerializer. You should use primitive type or plain object to store data. 

2011/11/30 胡 伟敏 <mynighte...@hotmail.com>

胡 伟敏

unread,
Nov 30, 2011, 2:56:14 AM11/30/11
to mongodb-user
Thank you....

On 11月30日, 下午3时44分, Nat Luengnaruemitchai <nat.lu...@gmail.com> wrote:
> BsonValue object doesn't support XmlSerializer. You should use primitive
> type or plain object to store data.
>

> 2011/11/30 胡 伟敏 <mynightelfpla...@hotmail.com>

Message has been deleted

胡 伟敏

unread,
Nov 30, 2011, 3:49:02 AM11/30/11
to mongodb-user
I write a new class which can be Serialize.....Instead of Serialize
BsonValue,BsonValueEx will be Serialized....


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;

namespace MagicMongoDBTool.Module
{
/// <summary>
/// 用于BsonValue的序列化
/// </summary>
[Serializable()]
public class BsonValueEx
{
public String mBsonType;
public String mBsonValue;
public BsonValueEx() {

}
public BsonValueEx(BsonValue value)
{
if (value.IsString)
{
mBsonType = "String";
mBsonValue = value.ToString();
}
}
public BsonValue GetBsonValue()
{
BsonValue Value = new BsonString("");
switch (mBsonType)
{
case "String":
Value = new BsonString(mBsonValue);
break;
default:
break;
}
return Value;
}
}
}


On 11月30日, 下午3时44分, Nat Luengnaruemitchai <nat.lu...@gmail.com> wrote:

> BsonValue object doesn't support XmlSerializer. You should use primitive
> type or plain object to store data.
>

> 2011/11/30 胡 伟敏 <mynightelfpla...@hotmail.com>

Reply all
Reply to author
Forward
0 new messages