public static IEnumerable <T> DeserializeElements <T>(this XmlNode root, string localName, string namespaceUri)
    {
        var serializer = XmlSerializerFactory.Create(typeof(T), localName, namespaceUri);
        var reader     = new XmlNodeReader(root);

        while (!reader.EOF)
        {
            if (!(reader.NodeType == XmlNodeType.Element && reader.LocalName == localName && reader.NamespaceURI == namespaceUri))
            {
                reader.ReadToFollowing(localName, namespaceUri);
            }

            if (!reader.EOF)
            {
                yield return((T)serializer.Deserialize(reader));
                // Note that the serializer will advance the reader past the end of the node
            }
        }
    }
Ejemplo n.º 2
0
        List <string> ThesaurusParse(string xml)
        {
            List <string> synonyms = new List <string>();
            XmlDocument   doc      = new XmlDocument();

            doc.LoadXml(xml);
            XmlReader reader = new XmlNodeReader(doc);

            while (reader.ReadToFollowing("w") && synonyms.Count < synonymNumber)
            {
                reader.MoveToAttribute("r");
                string type = reader.Value;
                reader.MoveToContent();
                string result = reader.ReadInnerXml();
                if (type == "syn")
                {
                    synonyms.Add(result);
                }
            }

            return(synonyms);
        }
Ejemplo n.º 3
0
 private string SendCommand(string message)
 {
     try
     {
         if (oNativeModeClient.Connected)
         {
             if (message != null && message.Length > 0)
             {
                 string response = oNativeModeClient.SendCommand(message.Trim());
             }
         }
         XmlNodeReader nrdr = oNativeModeClient.LastResponseReader;
         if (nrdr != null && nrdr.ReadToFollowing("Status"))
         {
             if (nrdr.ReadElementContentAsString().Equals("1"))
             {
                 return(nrdr.ReadElementContentAsString());
             }
         }
         else
         {
             return("未连接");
         }
     }
     catch (Cognex.InSight.NativeMode.CvsNativeModeResponseException resEx)
     {
         return("输入参数无效");
     }
     catch (Cognex.InSight.NativeMode.CvsNativeModeTimeoutException timEx)
     {
         return("超时");
     }
     catch (CvsNetworkException notConnected)
     {
         return("未连接");
     }
     return("未找到");
 }
Ejemplo n.º 4
0
        public Object ToObjct <T>(String binaryString)
        {
            XmlReader  reader_tmp;
            MethodInfo method;
            MethodInfo generic;

            XmlNode xmlDoc = xml.CreateElement("root");

            xmlDoc.InnerXml = binaryString;
            XmlReader reader = new XmlNodeReader(xmlDoc);

            reader.MoveToContent();
            reader.Read();



            if (reader.Value == "null")
            {
                return(null);
            }
            if (typeof(T) == typeof(DateTime))
            {
                while (reader.Name == "Value" || reader.NodeType != XmlNodeType.Text)
                {
                    reader.Read();
                }
                return
                    (Convert.ChangeType(
                         DateTime.ParseExact(reader.Value, "yyyy-MM-ddTHH:mm:ss.fffffffzzz", CultureInfo.InvariantCulture), typeof(T)));
            }
            if (typeof(T).IsPrimitive || typeof(T) == typeof(String))
            {
                while (reader.NodeType != XmlNodeType.Text && reader.GetAttribute("IsEmptyStr") != "true")
                {
                    reader.Read();
                }
                T tmpVar = (T)Convert.ChangeType(reader.Value, typeof(T));

                var asStr = tmpVar as string;
                if (asStr != null && (tmpVar as string) == "null")
                {
                    tmpVar = default(T);
                }


                //reader.ReadToFollowing("Value");
                //reader.Read();
                return(tmpVar);
            }
            if (typeof(T).IsArray)
            {
                while (reader.GetAttribute("IsArray") == null)
                {
                    reader.Read();
                }
                int size = Convert.ToInt32(reader.GetAttribute("Len"));
                reader.Read();

                var array = Array.CreateInstance(typeof(T).GetElementType(), size);


                for (int i = 0; i < size; i++)
                {
                    reader_tmp = reader.ReadSubtree();
                    reader_tmp.MoveToContent();

                    method  = typeof(Serial).GetMethod("ToObjct");
                    generic = method.MakeGenericMethod(Type.GetType(reader.GetAttribute("Type")));

                    var tmpint = generic.Invoke(this, new object[] { reader_tmp.ReadOuterXml() });
                    array.SetValue(tmpint, i);
                    reader.Read();
                }
                Convert.ChangeType(array, typeof(T));
                return(array);
            }
            T ReturnObject;

            ReturnObject = (T)Activator.CreateInstance(typeof(T));

            if (reader.Name == "RefToId" || reader.GetAttribute("IsToId") == "true")
            {
                if (reader.Name == "RefToId")
                {
                    var tmp = relList[Convert.ToInt32(reader.Value)];

                    reader.Skip();
                    return(tmp);
                }
                else if (reader.GetAttribute("IsToId") == "true")
                {
                    var tmp = relList[Convert.ToInt32(reader.GetAttribute("ToId"))];
                    reader.Skip();
                    reader.Read();
                    return(tmp);
                }
            }

            if (Id == 0)
            {
                relList.Add(0, ReturnObject);
                Id = -1;
            }
            else
            {
                if (Convert.ToInt32(reader.GetAttribute("Id")) == 0)
                {
                    return(null);
                }
                if (!relList.ContainsKey(Convert.ToInt32(reader.GetAttribute("Id"))))
                {
                    relList.Add(Convert.ToInt32(reader.GetAttribute("Id")), ReturnObject);
                }
            }



            FieldInfo[] fi =
                ReturnObject.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);


            for (int i = 0; i < fi.Length; i++)
            {
                while (reader.NodeType == XmlNodeType.EndElement || reader.Name != GetCleanFieldName(fi[i]))
                {
                    reader.Read();
                }

                if (IsReferenceType(fi[i].FieldType) && fi[i].FieldType != typeof(DateTime))
                {
                    bool RefFlag  = false;
                    bool ToIdFlag = false;
                    int  id;
                    int  toId;
                    while (!(reader.GetAttribute("IsReference") == "true" ||
                             reader.GetAttribute("IsToId") == "true" ||
                             reader.Name == "RefToId" ||
                             reader.Value == "null") ||
                           reader.NodeType == XmlNodeType.EndElement)
                    {
                        reader.Read();
                    }
                    if (reader.Value == "null")
                    {
                        return(null);
                    }
                    RefFlag  = reader.Name == "RefToId";
                    ToIdFlag = reader.GetAttribute("IsToId") == "true";
                    id       = Convert.ToInt32(reader.GetAttribute("Id"));
                    toId     = Convert.ToInt32(reader.GetAttribute("ToId"));


                    method  = typeof(Serial).GetMethod("ToObjct");
                    generic = method.MakeGenericMethod(fi[i].FieldType);



                    if (RefFlag)
                    {
                        var tmp = relList[Convert.ToInt32(reader.Value)];
                        fi[i].SetValue(ReturnObject, tmp);
                        reader.Skip();
                        reader.Read();
                    }
                    else if (ToIdFlag)
                    {
                        var tmp = relList[toId];
                        fi[i].SetValue(ReturnObject, tmp);
                        reader.Skip();
                    }
                    else
                    {
                        reader_tmp = reader.ReadSubtree();
                        reader_tmp.MoveToContent();


                        var tmp = generic.Invoke(this, new object[] { reader_tmp.ReadOuterXml() });
                        fi[i].SetValue(ReturnObject, tmp);
                    }


                    continue;
                }


                if (reader.Name != GetCleanFieldName(fi[i]))
                {
                    reader.ReadToFollowing(GetCleanFieldName(fi[i]));
                }
                reader_tmp = reader.ReadSubtree();
                reader_tmp.MoveToContent();
                method  = typeof(Serial).GetMethod("ToObjct");
                generic = method.MakeGenericMethod(fi[i].FieldType);

                var tmpint = generic.Invoke(this, new object[] { reader_tmp.ReadOuterXml() });
                fi[i].SetValue(ReturnObject, tmpint);
            }

            return(ReturnObject);
        }