示例#1
0
        private void OnSaveButtonClick(object sender, RoutedEventArgs e)
        {
            bool isSuccess = false;

            progressWindow = new SaveProgressWindow();
            bw             = new BackgroundWorker();
            //var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CognitiveLoop", "roboData.xml");

            bw.WorkerReportsProgress = true;
            bw.DoWork             += bw_DoWork;
            bw.ProgressChanged    += bw_ProgressChanged;
            bw.RunWorkerCompleted += bw_RunWorkerCompleted;
            bw.RunWorkerAsync();

            //Progress Bar Window
            progressWindow.Show();


            RoboViewModel robo = ((MainViewModel)this.DataContext).RoboViewModel;

            XmlSerializer serializer = new XmlSerializer(robo.GetType(), App.types);

            using (XmlTextWriter writer = new XmlTextWriter(App.xmlSource, System.Text.Encoding.UTF8))
            {
                serializer.Serialize(writer, robo);
            }
        }
示例#2
0
        private void AppendNodeRobo(Type[] types, RoboViewModel robo)
        {
            PropertyInfo[] listProperties = robo.GetType().GetProperties();
            foreach (var property in listProperties)
            {
                string nodeName = property.Name;
                writer.WriteStartElement(nodeName);
                var valueObj = property.GetValue(robo);

                if (valueObj != null)
                {
                    Type returnType = property.GetValue(robo).GetType();

                    foreach (Type type in types)
                    {
                        if (type == returnType)
                        {
                            var  castedObject = Convert.ChangeType(valueObj, type);
                            Type list         = this.isEnumerableType(type);
                            if (list != null)
                            {
                                IEnumerable enumerable = (IEnumerable)castedObject;
                                foreach (object item in enumerable)
                                {
                                    string className = item.GetType().Name;
                                    writer.WriteStartElement(className);

                                    AppendNodeSuits(types, item);

                                    writer.WriteEndElement();
                                }
                            }
                        }
                    }
                }
                else
                {
                    string value = "";
                    //XMLElement element = CreateXMLNode(nodeName, value, null);
                }

                writer.WriteEndElement();
            }
        }