Пример #1
0
        private static string GetProductsAsJson()
        {
            string json     = "";
            var    products = new ProductList();

            string appDataDirectory      = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\DesktopBridge.Samples\";
            string savedappDataDirectory = Windows.Storage.ApplicationData.Current.TemporaryFolder.Path;
            string savedappDataFilePath  = savedappDataDirectory + @"\" + filenName;

            appDataFilePath = appDataDirectory + filenName;

            //Load Product list from last save
            if (File.Exists(savedappDataFilePath))
            {
                File.Delete(appDataFilePath);
                File.Copy(savedappDataFilePath, appDataFilePath);
            }

            if (!File.Exists(appDataFilePath))
            {
                if (!Directory.Exists(appDataDirectory))
                {
                    Directory.CreateDirectory(appDataDirectory);
                }
                string        installDirectory = System.Reflection.Assembly.GetExecutingAssembly().Location;
                DirectoryInfo info             = Directory.GetParent(installDirectory);
                File.Copy(info.FullName + @"\" + filenName, appDataFilePath);
            }
            ds = new DataSet();
            ds.ReadXml(appDataFilePath);

            var reader = ds.CreateDataReader();

            while (reader.Read())
            {
                Product product = new Product();
                product.ProductID       = int.Parse((string)reader.GetValue(0));
                product.ProductName     = (string)reader.GetValue(1);
                product.QuantityPerUnit = (string)reader.GetValue(2);
                product.UnitPrice       = decimal.Parse((string)reader.GetValue(3));
                product.UnitsInStock    = int.Parse((string)reader.GetValue(4));
                products.Add(product);
                Console.WriteLine("Product: " + product.ProductName);
            }
            Console.WriteLine("Products count: {0}", products.Count);
            ds.Dispose();

            if (products.Count > 0)
            {
                Console.WriteLine("Serializing products...");
                JsonArray jsonArray = new JsonArray();

                foreach (Product prod in products)
                {
                    JsonObject jsonProd = new JsonObject();
                    jsonProd.Add("ProductID", JsonValue.CreateNumberValue(prod.ProductID));
                    jsonProd.Add("ProductName", JsonValue.CreateStringValue(prod.ProductName));
                    jsonProd.Add("QuantityPerUnit", JsonValue.CreateStringValue(prod.QuantityPerUnit));
                    jsonProd.Add("UnitPrice", JsonValue.CreateNumberValue((double)(prod.UnitPrice)));
                    jsonProd.Add("UnitsInStock", JsonValue.CreateNumberValue(prod.UnitsInStock));

                    jsonArray.Add(jsonProd);
                }

                json = jsonArray.Stringify();
            }

            return(json);
        }