FindElement() public method

public FindElement ( object ob, int index, object memberValue ) : System.Xml.Serialization.XmlTypeMapElementInfo
ob object
index int
memberValue object
return System.Xml.Serialization.XmlTypeMapElementInfo
Ejemplo n.º 1
0
        private object ReadListElement(XmlTypeMapping typeMap, bool isNullable, object list, bool canCreateInstance)
        {
            Type    type    = typeMap.TypeData.Type;
            ListMap listMap = (ListMap)typeMap.ObjectMap;

            if (type.IsArray && base.ReadNull())
            {
                return(null);
            }
            if (list == null)
            {
                if (!canCreateInstance || !typeMap.TypeData.HasPublicConstructor)
                {
                    throw base.CreateReadOnlyCollectionException(typeMap.TypeFullName);
                }
                list = this.CreateList(type);
            }
            if (base.Reader.IsEmptyElement)
            {
                base.Reader.Skip();
                if (type.IsArray)
                {
                    list = base.ShrinkArray((Array)list, 0, type.GetElementType(), false);
                }
                return(list);
            }
            int length = 0;

            base.Reader.ReadStartElement();
            base.Reader.MoveToContent();
            while (base.Reader.NodeType != XmlNodeType.EndElement)
            {
                if (base.Reader.NodeType == XmlNodeType.Element)
                {
                    XmlTypeMapElementInfo xmlTypeMapElementInfo = listMap.FindElement(base.Reader.LocalName, base.Reader.NamespaceURI);
                    if (xmlTypeMapElementInfo != null)
                    {
                        this.AddListValue(typeMap.TypeData, ref list, length++, this.ReadObjectElement(xmlTypeMapElementInfo), false);
                    }
                    else
                    {
                        base.UnknownNode(null);
                    }
                }
                else
                {
                    base.UnknownNode(null);
                }
                base.Reader.MoveToContent();
            }
            base.ReadEndElement();
            if (type.IsArray)
            {
                list = base.ShrinkArray((Array)list, length, type.GetElementType(), false);
            }
            return(list);
        }
		void WriteListContent (object container, TypeData listType, ListMap map, object ob, StringBuilder targetString)
		{
			if (listType.Type.IsArray)
			{
				Array array = (Array)ob;
				for (int n=0; n<array.Length; n++)
				{
					object item = array.GetValue (n);
					XmlTypeMapElementInfo info = map.FindElement (container, n, item);
					if (info != null && targetString == null) WriteMemberElement (info, item);
					else if (info != null && targetString != null) targetString.Append (GetStringValue (info.MappedType, info.TypeData, item)).Append (" ");
					else if (item != null) throw CreateUnknownTypeException (item);
				}
			}
			else if (ob is ICollection)
			{
				int count = (int) ob.GetType().GetProperty ("Count").GetValue(ob,null);
				PropertyInfo itemProp = TypeData.GetIndexerProperty (listType.Type);
				object[] index = new object[1];
				for (int n=0; n<count; n++)
				{
					index[0] = n;
					object item = itemProp.GetValue (ob, index);
					XmlTypeMapElementInfo info = map.FindElement (container, n, item);
					if (info != null && targetString == null) WriteMemberElement (info, item);
					else if (info != null && targetString != null) targetString.Append (GetStringValue (info.MappedType, info.TypeData, item)).Append (" ");
					else if (item != null) throw CreateUnknownTypeException (item);
				}
			}
			else if (ob is IEnumerable)
			{
				IEnumerable e = (IEnumerable)ob;
				foreach (object item in e)
				{
					XmlTypeMapElementInfo info = map.FindElement (container, -1, item);
					if (info != null && targetString == null) WriteMemberElement (info, item);
					else if (info != null && targetString != null) targetString.Append (GetStringValue (info.MappedType, info.TypeData, item)).Append (" ");
					else if (item != null) throw CreateUnknownTypeException (item);
				}
			}
			else
				throw new Exception ("Unsupported collection type");
		}
 void WriteListContent(object container, TypeData listType, ListMap map, object ob, StringBuilder targetString)
 {
     if (listType.Type.IsArray)
     {
         Array array = (Array)ob;
         for (int n = 0; n < array.Length; n++)
         {
             object item = array.GetValue(n);
             XmlTypeMapElementInfo info = map.FindElement(container, n, item);
             if (info != null && targetString == null)
             {
                 WriteMemberElement(info, item);
             }
             else if (info != null && targetString != null)
             {
                 targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" ");
             }
             else if (item != null)
             {
                 throw CreateUnknownTypeException(item);
             }
         }
     }
     else if (ob is ICollection)
     {
         int          count    = (int)ob.GetType().GetProperty("Count").GetValue(ob, null);
         PropertyInfo itemProp = TypeData.GetIndexerProperty(listType.Type);
         object[]     index    = new object[1];
         for (int n = 0; n < count; n++)
         {
             index[0] = n;
             object item = itemProp.GetValue(ob, index);
             XmlTypeMapElementInfo info = map.FindElement(container, n, item);
             if (info != null && targetString == null)
             {
                 WriteMemberElement(info, item);
             }
             else if (info != null && targetString != null)
             {
                 targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" ");
             }
             else if (item != null)
             {
                 throw CreateUnknownTypeException(item);
             }
         }
     }
     else if (ob is IEnumerable)
     {
         IEnumerable e = (IEnumerable)ob;
         foreach (object item in e)
         {
             XmlTypeMapElementInfo info = map.FindElement(container, -1, item);
             if (info != null && targetString == null)
             {
                 WriteMemberElement(info, item);
             }
             else if (info != null && targetString != null)
             {
                 targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" ");
             }
             else if (item != null)
             {
                 throw CreateUnknownTypeException(item);
             }
         }
     }
     else
     {
         throw new Exception("Unsupported collection type");
     }
 }
        object ReadListElement(XmlTypeMapping typeMap, bool isNullable, object list, bool canCreateInstance)
        {
            Type    listType = typeMap.TypeData.Type;
            ListMap listMap  = (ListMap)typeMap.ObjectMap;

            if (listType.IsArray && ReadNull())
            {
                return(null);
            }

            if (list == null)
            {
                if (canCreateInstance && typeMap.TypeData.HasPublicConstructor)
                {
                    list = CreateList(listType);
                }
                else
                {
                    throw CreateReadOnlyCollectionException(typeMap.TypeFullName);
                }
            }

            if (Reader.IsEmptyElement)
            {
                Reader.Skip();
                if (listType.IsArray)
                {
                    list = ShrinkArray((Array)list, 0, listType.GetElementType(), false);
                }
                return(list);
            }

            int index = 0;

            Reader.ReadStartElement();
            Reader.MoveToContent();

            while (Reader.NodeType != System.Xml.XmlNodeType.EndElement)
            {
                if (Reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    XmlTypeMapElementInfo elemInfo = listMap.FindElement(Reader.LocalName, Reader.NamespaceURI);
                    if (elemInfo != null)
                    {
                        AddListValue(typeMap.TypeData, ref list, index++, ReadObjectElement(elemInfo), false);
                    }
                    else
                    {
                        UnknownNode(null);
                    }
                }
                else
                {
                    UnknownNode(null);
                }

                Reader.MoveToContent();
            }
            ReadEndElement();

            if (listType.IsArray)
            {
                list = ShrinkArray((Array)list, index, listType.GetElementType(), false);
            }

            return(list);
        }
Ejemplo n.º 5
0
 private void WriteListContent(object container, TypeData listType, ListMap map, object ob, StringBuilder targetString)
 {
     if (listType.Type.IsArray)
     {
         Array array = (Array)ob;
         for (int i = 0; i < array.Length; i++)
         {
             object value = array.GetValue(i);
             XmlTypeMapElementInfo xmlTypeMapElementInfo = map.FindElement(container, i, value);
             if (xmlTypeMapElementInfo != null && targetString == null)
             {
                 this.WriteMemberElement(xmlTypeMapElementInfo, value);
             }
             else if (xmlTypeMapElementInfo != null && targetString != null)
             {
                 targetString.Append(this.GetStringValue(xmlTypeMapElementInfo.MappedType, xmlTypeMapElementInfo.TypeData, value)).Append(" ");
             }
             else if (value != null)
             {
                 throw base.CreateUnknownTypeException(value);
             }
         }
     }
     else if (ob is ICollection)
     {
         int          num             = (int)ob.GetType().GetProperty("Count").GetValue(ob, null);
         PropertyInfo indexerProperty = TypeData.GetIndexerProperty(listType.Type);
         object[]     array2          = new object[1];
         for (int j = 0; j < num; j++)
         {
             array2[0] = j;
             object value2 = indexerProperty.GetValue(ob, array2);
             XmlTypeMapElementInfo xmlTypeMapElementInfo2 = map.FindElement(container, j, value2);
             if (xmlTypeMapElementInfo2 != null && targetString == null)
             {
                 this.WriteMemberElement(xmlTypeMapElementInfo2, value2);
             }
             else if (xmlTypeMapElementInfo2 != null && targetString != null)
             {
                 targetString.Append(this.GetStringValue(xmlTypeMapElementInfo2.MappedType, xmlTypeMapElementInfo2.TypeData, value2)).Append(" ");
             }
             else if (value2 != null)
             {
                 throw base.CreateUnknownTypeException(value2);
             }
         }
     }
     else
     {
         if (!(ob is IEnumerable))
         {
             throw new Exception("Unsupported collection type");
         }
         IEnumerable enumerable = (IEnumerable)ob;
         foreach (object obj in enumerable)
         {
             XmlTypeMapElementInfo xmlTypeMapElementInfo3 = map.FindElement(container, -1, obj);
             if (xmlTypeMapElementInfo3 != null && targetString == null)
             {
                 this.WriteMemberElement(xmlTypeMapElementInfo3, obj);
             }
             else if (xmlTypeMapElementInfo3 != null && targetString != null)
             {
                 targetString.Append(this.GetStringValue(xmlTypeMapElementInfo3.MappedType, xmlTypeMapElementInfo3.TypeData, obj)).Append(" ");
             }
             else if (obj != null)
             {
                 throw base.CreateUnknownTypeException(obj);
             }
         }
     }
 }