本文整理汇总了C#中YAXLib.YAXSerializer.SetNamespaceToOverrideEmptyNamespace方法的典型用法代码示例。如果您正苦于以下问题:C# YAXSerializer.SetNamespaceToOverrideEmptyNamespace方法的具体用法?C# YAXSerializer.SetNamespaceToOverrideEmptyNamespace怎么用?C# YAXSerializer.SetNamespaceToOverrideEmptyNamespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类YAXLib.YAXSerializer
的用法示例。
在下文中一共展示了YAXSerializer.SetNamespaceToOverrideEmptyNamespace方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RetreiveElementValue
//.........这里部分代码省略.........
}
}
else if (memberType == typeof(string))
{
if (String.IsNullOrEmpty(elemValue) && xelemValue != null)
{
if (xelemValue.IsEmpty)
elemValue = null;
else
elemValue = String.Empty;
}
try
{
member.SetValue(o, elemValue);
}
catch
{
this.OnExceptionOccurred(new YAXPropertyCannotBeAssignedTo(member.Alias.LocalName), this.m_defaultExceptionType);
}
}
else if (ReflectionUtils.IsBasicType(memberType))
{
object convertedObj;
if (ReflectionUtils.IsNullable(memberType) && String.IsNullOrEmpty(elemValue))
{
convertedObj = member.DefaultValue;
}
else
{
convertedObj = ReflectionUtils.ConvertBasicType(elemValue, memberType);
}
try
{
try
{
member.SetValue(o, convertedObj);
}
catch
{
this.OnExceptionOccurred(new YAXPropertyCannotBeAssignedTo(member.Alias.LocalName), m_defaultExceptionType);
}
}
catch (Exception ex)
{
if (ex is YAXException)
{
throw;
}
this.OnExceptionOccurred(new YAXBadlyFormedInput(member.Alias.LocalName, elemValue), member.TreatErrorsAs);
try
{
member.SetValue(o, member.DefaultValue);
}
catch
{
this.OnExceptionOccurred(new YAXDefaultValueCannotBeAssigned(member.Alias.LocalName, member.DefaultValue), m_defaultExceptionType);
}
}
}
else if (member.IsTreatedAsDictionary && member.DictionaryAttributeInstance != null)
{
DeserializeTaggedDictionaryMember(o, member, xelemValue);
}
else if (member.IsTreatedAsCollection)
{
DeserializeCollectionMember(o, member, memberType, elemValue, xelemValue);
}
else
{
var ser = new YAXSerializer(memberType, m_exceptionPolicy, m_defaultExceptionType, m_serializationOption);
ser.SetNamespaceToOverrideEmptyNamespace(
member.Namespace.
IfEmptyThen(this.TypeNamespace).
IfEmptyThenNone());
ser.IsCraetedToDeserializeANonCollectionMember = !(member.IsTreatedAsDictionary || member.IsTreatedAsCollection);
if (m_desObject != null) // i.e. it is in resuming mode
{
ser.SetDeserializationBaseObject(member.GetValue(o));
}
object convertedObj = ser.DeserializeBase(xelemValue);
m_parsingErrors.AddRange(ser.ParsingErrors);
try
{
member.SetValue(o, convertedObj);
}
catch
{
this.OnExceptionOccurred(new YAXPropertyCannotBeAssignedTo(member.Alias.LocalName), this.m_defaultExceptionType);
}
}
}
示例2: SerializeBase
/// <summary>
/// One of the base methods that perform the whole job of serialization.
/// </summary>
/// <param name="obj">The object to be serialized</param>
/// <returns>an instance of <c>XElement</c> which contains the result of
/// serialization of the specified object</returns>
private XElement SerializeBase(object obj)
{
if (!m_type.IsInstanceOfType(obj))
{
throw new YAXObjectTypeMismatch(m_type, obj.GetType());
}
// to serialize stand-alone collection or dictionary objects
if (m_udtWrapper.IsTreatedAsDictionary)
{
var elemResult = MakeDictionaryElement(null, m_udtWrapper.Alias, obj,
m_udtWrapper.DictionaryAttributeInstance, m_udtWrapper.CollectionAttributeInstance);
if (m_udtWrapper.PreservesWhitespace)
XMLUtils.AddPreserveSpaceAttribute(elemResult);
return elemResult;
}
else if (m_udtWrapper.IsTreatedAsCollection)
{
var elemResult = MakeCollectionElement(null, m_udtWrapper.Alias, obj, null, null);
if (m_udtWrapper.PreservesWhitespace)
XMLUtils.AddPreserveSpaceAttribute(elemResult);
return elemResult;
}
else if(ReflectionUtils.IsBasicType(m_udtWrapper.UnderlyingType))
{
bool dummyAlreadyAdded;
var elemResult = MakeBaseElement(null, m_udtWrapper.Alias, obj, out dummyAlreadyAdded);
if (m_udtWrapper.PreservesWhitespace)
XMLUtils.AddPreserveSpaceAttribute(elemResult);
return elemResult;
}
else if(!m_udtWrapper.UnderlyingType.EqualsOrIsNullableOf(obj.GetType()))
{
// this block of code runs if the serializer is instantiated with a
// another base value such as System.Object but is provided with an
// object of its child
var ser = new YAXSerializer(obj.GetType(), m_exceptionPolicy,
m_defaultExceptionType, m_serializationOption);
ser.SetNamespaceToOverrideEmptyNamespace(TypeNamespace);
//ser.SetBaseElement(insertionLocation);
var xdoc = ser.SerializeToXDocument(obj);
var elem = xdoc.Root;
ImportNamespaces(ser);
m_parsingErrors.AddRange(ser.ParsingErrors);
elem.Name = m_udtWrapper.Alias;
elem.AddAttributeNamespaceSafe(m_yaxLibNamespaceUri + m_trueTypeAttrName, obj.GetType().FullName);
RegisterYaxLibNamespace();
AddNamespacesToElement(elem);
return elem;
}
else
{
return SerializeBase(obj, m_udtWrapper.Alias);
}
}
示例3: DeserializeTaggedDictionaryValue
private object DeserializeTaggedDictionaryValue(XElement xelemValue, XName alias, Type type,
YAXCollectionAttribute colAttributeInstance, YAXDictionaryAttribute dicAttrInstance)
{
// otherwise the "else if(member.IsTreatedAsCollection)" block solves the problem
Type keyType, valueType;
if (!ReflectionUtils.IsIDictionary(type, out keyType, out valueType))
{
throw new Exception("elemValue must be a Dictionary");
}
Type pairType = null;
ReflectionUtils.IsIEnumerable(type, out pairType);
XName eachElementName = StringUtils.RefineSingleElement(ReflectionUtils.GetTypeFriendlyName(pairType));
bool isKeyAttrib = false;
bool isValueAttrib = false;
bool isKeyContent = false;
bool isValueContent = false;
XName keyAlias = alias.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone() + "Key";
XName valueAlias = alias.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone() + "Value";
if (colAttributeInstance != null && colAttributeInstance.EachElementName != null)
{
eachElementName = StringUtils.RefineSingleElement(colAttributeInstance.EachElementName);
eachElementName = eachElementName.OverrideNsIfEmpty(alias.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone());
}
if (dicAttrInstance != null)
{
if (dicAttrInstance.EachPairName != null)
{
eachElementName = StringUtils.RefineSingleElement(dicAttrInstance.EachPairName);
eachElementName = eachElementName.OverrideNsIfEmpty(alias.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone());
}
if (dicAttrInstance.SerializeKeyAs == YAXNodeTypes.Attribute)
{
isKeyAttrib = ReflectionUtils.IsBasicType(keyType);
}
else if (dicAttrInstance.SerializeKeyAs == YAXNodeTypes.Content)
{
isKeyContent = ReflectionUtils.IsBasicType(keyType);
}
if (dicAttrInstance.SerializeValueAs == YAXNodeTypes.Attribute)
{
isValueAttrib = ReflectionUtils.IsBasicType(valueType);
}
else if (dicAttrInstance.SerializeValueAs == YAXNodeTypes.Content)
{
isValueContent = ReflectionUtils.IsBasicType(valueType);
}
if (dicAttrInstance.KeyName != null)
{
keyAlias = StringUtils.RefineSingleElement(dicAttrInstance.KeyName);
keyAlias = keyAlias.OverrideNsIfEmpty(alias.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone());
}
if (dicAttrInstance.ValueName != null)
{
valueAlias = StringUtils.RefineSingleElement(dicAttrInstance.ValueName);
valueAlias = valueAlias.OverrideNsIfEmpty(alias.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone());
}
}
object dic = type.InvokeMember(string.Empty, System.Reflection.BindingFlags.CreateInstance, null, null, new object[0]);
foreach (XElement childElem in xelemValue.Elements(eachElementName))
{
object key = null, value = null;
YAXSerializer keySer = null, valueSer = null;
bool isKeyFound = VerifyDictionaryPairElements(ref keyType, ref isKeyAttrib, ref isKeyContent, keyAlias, childElem);
bool isValueFound = VerifyDictionaryPairElements(ref valueType, ref isValueAttrib, ref isValueContent, valueAlias, childElem);
if (!isKeyFound && !isValueFound)
continue;
if (isKeyFound)
{
if (isKeyAttrib)
{
key = ReflectionUtils.ConvertBasicType(childElem.Attribute_NamespaceSafe(keyAlias).Value, keyType);
}
else if (isKeyContent)
{
key = ReflectionUtils.ConvertBasicType(childElem.GetXmlContent(), keyType);
}
else if (ReflectionUtils.IsBasicType(keyType))
{
key = ReflectionUtils.ConvertBasicType(childElem.Element(keyAlias).Value, keyType);
}
else
{
if (keySer == null)
{
keySer = new YAXSerializer(keyType, m_exceptionPolicy, m_defaultExceptionType, m_serializationOption);
keySer.SetNamespaceToOverrideEmptyNamespace(keyAlias.Namespace);
}
key = keySer.DeserializeBase(childElem.Element(keyAlias));
//.........这里部分代码省略.........
示例4: MakeBaseElement
/// <summary>
/// Makes an XML element with the specified name, corresponding to the object specified.
/// </summary>
/// <param name="insertionLocation">The insertion location.</param>
/// <param name="name">The name of the element.</param>
/// <param name="value">The object to be serialized in an XML element.</param>
/// <param name="alreadyAdded">if set to <c>true</c> specifies the element returned is
/// already added to the parent element and should not be added once more.</param>
/// <returns>
/// an instance of <c>XElement</c> which will contain the serialized object,
/// or <c>null</c> if the serialized object is already added to the base element
/// </returns>
private XElement MakeBaseElement(XElement insertionLocation, XName name, object value, out bool alreadyAdded)
{
alreadyAdded = false;
if (value == null || ReflectionUtils.IsBasicType(value.GetType()))
{
if (value != null)
value = value.ToXmlValue();
return new XElement(name, value);
}
else if (ReflectionUtils.IsStringConvertibleIFormattable(value.GetType()))
{
object elementValue = value.GetType().InvokeMember("ToString", BindingFlags.InvokeMethod, null, value, new object[0]);
return new XElement(name, elementValue);
}
else
{
YAXSerializer ser = new YAXSerializer(value.GetType(), m_exceptionPolicy, m_defaultExceptionType, m_serializationOption);
ser.SetNamespaceToOverrideEmptyNamespace(name.Namespace);
ser.SetBaseElement(insertionLocation);
XElement elem = ser.SerializeBase(value, name);
ImportNamespaces(ser);
m_parsingErrors.AddRange(ser.ParsingErrors);
alreadyAdded = true;
return elem;
}
}
示例5: DeserializeKeyValuePair
/// <summary>
/// Deserializes the XML reperesentation of a key-value pair, as specified, and returns
/// a <c>KeyValuePair</c> instance containing the deserialized data.
/// </summary>
/// <param name="baseElement">The element contating the XML reperesentation of a key-value pair.</param>
/// <returns>a <c>KeyValuePair</c> instance containing the deserialized data</returns>
private object DeserializeKeyValuePair(XElement baseElement)
{
Type[] genArgs = m_type.GetGenericArguments();
Type keyType = genArgs[0];
Type valueType = genArgs[1];
XName xnameKey = TypeNamespace.IfEmptyThenNone() + "Key";
XName xnameValue = TypeNamespace.IfEmptyThenNone() + "Value";
object keyValue, valueValue;
if (ReflectionUtils.IsBasicType(keyType))
{
try
{
keyValue = ReflectionUtils.ConvertBasicType(
baseElement.Element(xnameKey).Value, keyType);
}
catch (NullReferenceException)
{
keyValue = null;
}
}
else if (ReflectionUtils.IsStringConvertibleIFormattable(keyType))
{
keyValue = keyType.InvokeMember(string.Empty,
BindingFlags.CreateInstance,
null, null,
new object[] { baseElement.Element(xnameKey).Value });
}
else if (ReflectionUtils.IsCollectionType(keyType))
{
keyValue = DeserializeCollectionValue(keyType,
baseElement.Element(xnameKey), xnameKey, null);
}
else
{
var ser = new YAXSerializer(keyType, m_exceptionPolicy,
m_defaultExceptionType, m_serializationOption);
ser.SetNamespaceToOverrideEmptyNamespace(xnameKey.Namespace.IfEmptyThenNone());
keyValue = ser.DeserializeBase(baseElement.Element(xnameKey));
m_parsingErrors.AddRange(ser.ParsingErrors);
}
if (ReflectionUtils.IsBasicType(valueType))
{
try
{
valueValue = ReflectionUtils.ConvertBasicType(
baseElement.Element(xnameValue).Value, valueType);
}
catch (NullReferenceException)
{
valueValue = null;
}
}
else if (ReflectionUtils.IsStringConvertibleIFormattable(valueType))
{
valueValue = valueType.InvokeMember(string.Empty, BindingFlags.CreateInstance,
null, null, new object[] { baseElement.Element(xnameValue).Value });
}
else if (ReflectionUtils.IsCollectionType(valueType))
{
valueValue = DeserializeCollectionValue(valueType,
baseElement.Element(xnameValue), xnameValue, null);
}
else
{
var ser = new YAXSerializer(valueType, m_exceptionPolicy,
m_defaultExceptionType, m_serializationOption);
ser.SetNamespaceToOverrideEmptyNamespace(xnameValue.Namespace.IfEmptyThenNone());
valueValue = ser.DeserializeBase(baseElement.Element(xnameValue));
m_parsingErrors.AddRange(ser.ParsingErrors);
}
object pair = m_type.InvokeMember(string.Empty,
System.Reflection.BindingFlags.CreateInstance,
null, null, new object[] { keyValue, valueValue });
return pair;
}
示例6: DeserializeCollectionValue
/// <summary>
/// Retreives the collection value.
/// </summary>
/// <param name="colType">Type of the collection to be retrieved.</param>
/// <param name="xelemValue">The value of xml element.</param>
/// <param name="memberAlias">The member's alias, used only in exception titles.</param>
/// <param name="colAttrInstance">The collection attribute instance.</param>
/// <returns></returns>
private object DeserializeCollectionValue(Type colType, XElement xelemValue, XName memberAlias, YAXCollectionAttribute colAttrInstance)
{
var lst = new List<object>(); // this will hold the actual data items
Type itemType = ReflectionUtils.GetCollectionItemType(colType);
if (ReflectionUtils.IsBasicType(itemType) && colAttrInstance != null && colAttrInstance.SerializationType == YAXCollectionSerializationTypes.Serially)
{
// What if the collection was serialized serially
char[] seps = colAttrInstance.SeparateBy.ToCharArray();
// can white space characters be added to the separators?
if (colAttrInstance == null || colAttrInstance.IsWhiteSpaceSeparator)
{
seps = seps.Union(new [] { ' ', '\t', '\r', '\n' }).ToArray();
}
string elemValue = xelemValue.Value;
string[] items = elemValue.Split(seps, StringSplitOptions.RemoveEmptyEntries);
foreach (string wordItem in items)
{
try
{
lst.Add(ReflectionUtils.ConvertBasicType(wordItem, itemType));
}
catch
{
OnExceptionOccurred(new YAXBadlyFormedInput(memberAlias.ToString(), elemValue), m_defaultExceptionType);
}
}
}
else
{
//What if the collection was serialized recursive
bool isPrimitive = false;
if (ReflectionUtils.IsBasicType(itemType))
{
isPrimitive = true;
}
XName eachElemName = null;
if (colAttrInstance != null && colAttrInstance.EachElementName != null)
{
eachElemName = StringUtils.RefineSingleElement(colAttrInstance.EachElementName);
eachElemName = eachElemName.OverrideNsIfEmpty(memberAlias.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone());
}
var elemsToSearch = eachElemName == null ? xelemValue.Elements() : xelemValue.Elements(eachElemName);
foreach (XElement childElem in elemsToSearch)
{
Type curElementType = itemType;
bool curElementIsPrimitive = isPrimitive;
XAttribute realTypeAttribute = childElem.Attribute_NamespaceSafe(m_yaxLibNamespaceUri + m_trueTypeAttrName);
if (realTypeAttribute != null)
{
Type theRealType = ReflectionUtils.GetTypeByName(realTypeAttribute.Value);
if (theRealType != null)
{
curElementType = theRealType;
curElementIsPrimitive = ReflectionUtils.IsBasicType(curElementType);
}
}
// TODO: check if curElementType is derived or is the same is itemType, for speed concerns perform this check only when elementName is null
if (eachElemName == null && (curElementType == typeof(object) || !ReflectionUtils.IsTypeEqualOrInheritedFromType(curElementType, itemType)))
continue;
if (curElementIsPrimitive)
{
try
{
lst.Add(ReflectionUtils.ConvertBasicType(childElem.Value, curElementType));
}
catch
{
this.OnExceptionOccurred(new YAXBadlyFormedInput(childElem.Name.ToString(), childElem.Value), m_defaultExceptionType);
}
}
else
{
var ser = new YAXSerializer(curElementType, m_exceptionPolicy, m_defaultExceptionType, m_serializationOption);
ser.SetNamespaceToOverrideEmptyNamespace(
memberAlias.Namespace.
IfEmptyThen(this.TypeNamespace).
IfEmptyThenNone());
lst.Add(ser.DeserializeBase(childElem));
m_parsingErrors.AddRange(ser.ParsingErrors);
}
//.........这里部分代码省略.........
示例7: MakeDictionaryElement
/// <summary>
/// Creates a dictionary element according to the specified options, as described
/// by the attribute instances.
/// </summary>
/// <param name="insertionLocation">The insertion location.</param>
/// <param name="elementName">Name of the element.</param>
/// <param name="elementValue">The element value, corresponding to a dictionary object.</param>
/// <param name="dicAttrInst">reference to the dictionary attribute instance.</param>
/// <param name="collectionAttrInst">reference to collection attribute instance.</param>
/// <returns>
/// an instance of <c>XElement</c> which contains the dictionary object
/// serialized properly
/// </returns>
private XElement MakeDictionaryElement(
XElement insertionLocation,
XName elementName,
object elementValue,
YAXDictionaryAttribute dicAttrInst,
YAXCollectionAttribute collectionAttrInst)
{
if (elementValue == null)
{
return new XElement(elementName);
}
Type keyType, valueType;
if (!ReflectionUtils.IsIDictionary(elementValue.GetType(), out keyType, out valueType))
{
throw new ArgumentException("elementValue must be a Dictionary");
}
// serialize other non-collection members
var ser = new YAXSerializer(elementValue.GetType(), m_exceptionPolicy, m_defaultExceptionType, m_serializationOption);
ser.m_documentDefaultNamespace = m_documentDefaultNamespace;
ser.SetNamespaceToOverrideEmptyNamespace(elementName.Namespace);
ser.SetBaseElement(insertionLocation);
XElement elem = ser.SerializeBase(elementValue, elementName);
ImportNamespaces(ser);
m_parsingErrors.AddRange(ser.ParsingErrors);
// now iterate through collection members
var dicInst = elementValue as IEnumerable;
bool isKeyAttrib = false;
bool isValueAttrib = false;
bool isKeyContent = false;
bool isValueContent = false;
string keyFormat = null;
string valueFormat = null;
XName keyAlias = elementName.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone() + "Key";
XName valueAlias = elementName.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone() + "Value";
XName eachElementName = null;
if (collectionAttrInst != null && !String.IsNullOrEmpty(collectionAttrInst.EachElementName))
{
eachElementName = StringUtils.RefineSingleElement(collectionAttrInst.EachElementName);
if (eachElementName.Namespace.IsEmpty())
RegisterNamespace(eachElementName.Namespace, null);
eachElementName = eachElementName.OverrideNsIfEmpty(elementName.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone());
}
if (dicAttrInst != null)
{
if (dicAttrInst.EachPairName != null)
{
eachElementName = StringUtils.RefineSingleElement(dicAttrInst.EachPairName);
if (eachElementName.Namespace.IsEmpty())
RegisterNamespace(eachElementName.Namespace, null);
eachElementName = eachElementName.OverrideNsIfEmpty(elementName.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone());
}
if (dicAttrInst.SerializeKeyAs == YAXNodeTypes.Attribute)
{
isKeyAttrib = ReflectionUtils.IsBasicType(keyType);
}
else if (dicAttrInst.SerializeKeyAs == YAXNodeTypes.Content)
{
isKeyContent = ReflectionUtils.IsBasicType(keyType);
}
if (dicAttrInst.SerializeValueAs == YAXNodeTypes.Attribute)
{
isValueAttrib = ReflectionUtils.IsBasicType(valueType);
}
else if (dicAttrInst.SerializeValueAs == YAXNodeTypes.Content)
{
isValueContent = ReflectionUtils.IsBasicType(valueType);
}
keyFormat = dicAttrInst.KeyFormatString;
valueFormat = dicAttrInst.ValueFormatString;
keyAlias = StringUtils.RefineSingleElement(dicAttrInst.KeyName ?? "Key");
if (keyAlias.Namespace.IsEmpty())
RegisterNamespace(keyAlias.Namespace, null);
keyAlias = keyAlias.OverrideNsIfEmpty(elementName.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone());
valueAlias = StringUtils.RefineSingleElement(dicAttrInst.ValueName ?? "Value");
if (valueAlias.Namespace.IsEmpty())
RegisterNamespace(valueAlias.Namespace, null);
//.........这里部分代码省略.........
示例8: MakeCollectionElement
/// <summary>
/// Serializes a collection object.
/// </summary>
/// <param name="insertionLocation">The insertion location.</param>
/// <param name="elementName">Name of the element.</param>
/// <param name="elementValue">The object to be serailized.</param>
/// <param name="collectionAttrInst">The collection attribute instance.</param>
/// <param name="format">formatting string, which is going to be applied to all members of the collection.</param>
/// <returns>
/// an instance of <c>XElement</c> which will contain the serailized collection
/// </returns>
private XElement MakeCollectionElement(
XElement insertionLocation, XName elementName, object elementValue,
YAXCollectionAttribute collectionAttrInst, string format)
{
if (elementValue == null)
{
return new XElement(elementName);
}
if (!(elementValue is IEnumerable))
{
throw new ArgumentException("elementValue must be an IEnumerable");
}
// serialize other non-collection members
var ser = new YAXSerializer(elementValue.GetType(), m_exceptionPolicy, m_defaultExceptionType, m_serializationOption);
ser.m_documentDefaultNamespace = m_documentDefaultNamespace;
ser.SetNamespaceToOverrideEmptyNamespace(elementName.Namespace);
ser.SetBaseElement(insertionLocation);
XElement elemToAdd = ser.SerializeBase(elementValue, elementName);
ImportNamespaces(ser);
m_parsingErrors.AddRange(ser.ParsingErrors);
// now iterate through collection members
var collectionInst = elementValue as IEnumerable;
var serType = YAXCollectionSerializationTypes.Recursive;
string seperator = string.Empty;
XName eachElementName = null;
if (collectionAttrInst != null)
{
serType = collectionAttrInst.SerializationType;
seperator = collectionAttrInst.SeparateBy;
if (collectionAttrInst.EachElementName != null)
{
eachElementName = StringUtils.RefineSingleElement(collectionAttrInst.EachElementName);
if (eachElementName.Namespace.IsEmpty())
RegisterNamespace(eachElementName.Namespace, null);
eachElementName = eachElementName.OverrideNsIfEmpty(elementName.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone());
}
}
Type colItemType = ReflectionUtils.GetCollectionItemType(elementValue.GetType());
if (serType == YAXCollectionSerializationTypes.Serially && !ReflectionUtils.IsBasicType(colItemType))
serType = YAXCollectionSerializationTypes.Recursive;
UdtWrapper colItemsUdt = TypeWrappersPool.Pool.GetTypeWrapper(colItemType, this);
if (serType == YAXCollectionSerializationTypes.Serially && elemToAdd.IsEmpty)
{
var sb = new StringBuilder();
bool isFirst = true;
object objToAdd = null;
foreach (object obj in collectionInst)
{
if (colItemsUdt.IsEnum)
objToAdd = colItemsUdt.EnumWrapper.GetAlias(obj);
else if (format != null)
objToAdd = ReflectionUtils.TryFormatObject(obj, format);
else
objToAdd = obj;
if (isFirst)
{
sb.Append(objToAdd.ToXmlValue());
isFirst = false;
}
else
{
sb.AppendFormat(CultureInfo.InvariantCulture, "{0}{1}", seperator, objToAdd);
}
}
bool alreadyAdded = false;
elemToAdd = MakeBaseElement(insertionLocation, elementName, sb.ToString(), out alreadyAdded);
if (alreadyAdded)
elemToAdd = null;
}
else
{
//var elem = new XElement(elementName, null);
object objToAdd = null;
foreach (object obj in collectionInst)
{
objToAdd = (format == null) ? obj : ReflectionUtils.TryFormatObject(obj, format);
//.........这里部分代码省略.........
示例9: NewInternalSerializer
private YAXSerializer NewInternalSerializer(Type type, XNamespace namespaceToOverride, XElement insertionLocation)
{
var serializer = new YAXSerializer(type, m_exceptionPolicy, m_defaultExceptionType, m_serializationOption);
serializer.MaxRecursion = MaxRecursion == 0 ? 0 : MaxRecursion - 1;
serializer.m_serializedStack = m_serializedStack;
serializer.m_documentDefaultNamespace = m_documentDefaultNamespace;
if(namespaceToOverride != null)
serializer.SetNamespaceToOverrideEmptyNamespace(namespaceToOverride);
if(insertionLocation != null)
serializer.SetBaseElement(insertionLocation);
return serializer;
}