Exemple #1
0
        public static object ParseXml(XmlReader reader, Type ObjectType, MemoryDo memoryDo, LinkedList <IGWObject> buffer = null)
        {
            buffer = buffer ?? new LinkedList <IGWObject>();
            var constr = ObjectType.GetConstructor(new Type[0]);
            var t      = constr.Invoke(new object[0]) as IGWObject;

            //while (reader.Read())
            //{
            //    // prüfen, ob es sich aktuell um ein Element handelt
            //    if (reader.NodeType == XmlNodeType.Element)
            //    {
            //        break;
            //    }
            //}

            var obj = ParseXml(t, reader, null, null, null, memoryDo, buffer);

            buffer.Add(t);
            memoryDo.Trigger(t.GWId.ToString());

            return(t);
        }
Exemple #2
0
        public static object ParseXml(object t, XmlReader reader, Type innerType, MethodInfo addToCurrentCollection, IEnumerable <IGWObject> tempCollection, MemoryDo memoryDo, LinkedList <IGWObject> buffer = null)
        {
            DebugDepth++;



            var ObjectType = t.GetType();
            var props      = (ObjectType.GetProperties()).ToList();

            while (reader.Read())
            {
                // prüfen, ob es sich aktuell um ein Element handelt
                if (reader.NodeType == XmlNodeType.Element)
                {
                    var prop = props.FirstOrDefault(p => p.Name.Equals(reader.Name));

                    if (DebugDepth != reader.Depth)
                    {
                    }

                    if (reader.IsEmptyElement)
                    {
                        if (reader.HasAttributes)
                        {
                            if (prop != null && prop.CanWrite)
                            {
                                reader.MoveToNextAttribute();
                                var id       = reader.Value;
                                var propdata = buffer.FirstOrDefault(obj => obj.GWId.ToString().Equals(id));
                                prop.SetValue(t, propdata);

                                if (propdata == null)
                                {
                                    memoryDo.Add(reader.Value, prop, id, (propI, idI) => propI.SetValue(t, buffer.FirstOrDefault(obj => obj.GWId.ToString().Equals(idI))));
                                }
                            }
                            else if (reader.Name.StartsWith("Item") && addToCurrentCollection != null)
                            {
                                reader.MoveToNextAttribute();
                                var propdata = buffer.FirstOrDefault(obj => obj.GWId.ToString().Equals(reader.Value));
                                addToCurrentCollection.Invoke(tempCollection, new object[] { propdata });

                                if (propdata == null)
                                {
                                    memoryDo.Add(reader.Value, () => addToCurrentCollection.Invoke(tempCollection, new object[] { propdata }));
                                }
                            }
                            continue;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if (prop != null)
                    {
                        var value    = prop.GetValue(t);
                        var proptype = prop.PropertyType;
                        if (SimpleTypes.Contains(proptype))
                        {
                            reader.Read();
                            if (prop.CanWrite)
                            {
                                prop.SetValue(t, ConvertFromString(proptype, reader.Value));// reader.ReadContentAs(proptype, null));
                            }
                            reader.Read();
                        }
                        else if (value is ILogic)
                        {
                            if (reader.HasAttributes)
                            {
                                reader.MoveToNextAttribute();
                                (value as IGWObject).GWId = Guid.Parse(reader.Value);
                            }
                            ParseXml(value, reader, null, null, null, memoryDo, buffer);
                        }
                        else if (proptype.GetInterfaces().Contains(typeof(IGWObject)) && (prop.CanWrite || value != null))
                        {
                            if (prop.CanWrite && value == null)
                            {
                                if (reader.HasAttributes)
                                {
                                    reader.MoveToNextAttribute();
                                    var propdata = buffer.FirstOrDefault(obj => obj.GWId.ToString().Equals(reader.Value));
                                    prop.SetValue(t, propdata);
                                }
                                else
                                {
                                    prop.SetValue(t, ParseXml(reader, proptype, memoryDo, buffer));
                                }
                            }
                            else
                            {
                                //ParseXml(value, reader, value.GetType(),
                                ParseXml(value, reader, null, null, null, memoryDo, buffer);
                                //value.TakeSimpleValues(ParseXml(reader, proptype, buffer), proptype);
                            }
                        }
                        else if (value is IEnumerable <IGWObject> )
                        {
                            innerType = value.GetType().GenericTypeArguments.FirstOrDefault();
                            addToCurrentCollection = value.GetType().GetMethod("Add");
                            tempCollection         = value as IEnumerable <IGWObject>;
                            ParseXml(t, reader, innerType, addToCurrentCollection, tempCollection, memoryDo, buffer);
                        }
                    }
                    else if (reader.Name.StartsWith("Item") && addToCurrentCollection != null)
                    {
                        if (reader.HasAttributes)
                        {
                            reader.MoveToNextAttribute();
                            var propdata = buffer.FirstOrDefault(obj => obj.GWId.ToString().Equals(reader.Value));
                            if (propdata == null)
                            {
                                var constr2 = innerType.GetConstructor(new Type[0]);
                                var t2      = constr2.Invoke(new object[0]) as IGWObject;
                                t2.GWId = Guid.Parse(reader.Value);
                                var item = ParseXml(t2, reader, null, null, null, memoryDo, buffer);
                                buffer.Add(t2);
                                memoryDo.Trigger(t2.GWId.ToString());
                                addToCurrentCollection.Invoke(tempCollection, new object[] { item });
                            }
                            else
                            {
                                addToCurrentCollection.Invoke(tempCollection, new object[] { propdata });
                            }
                        }
                        else
                        {
                            var constr2 = innerType.GetConstructor(new Type[0]);
                            var t2      = constr2.Invoke(new object[0]) as IGWObject;
                            var item    = ParseXml(t2, reader, null, null, null, memoryDo, buffer);
                            buffer.Add(t2);
                            memoryDo.Trigger(t2.GWId.ToString());
                            addToCurrentCollection.Invoke(tempCollection, new object[] { item });
                        }
                    }
                }
                else if (reader.NodeType == XmlNodeType.EndElement)
                {
                    var nm = reader.Name;
                    DebugDepth--;
                    return(t);
                }
            }
            return(t);
        }