I am having trouble figuring out how to write this collection out to file. I have the following classes
public static class GeoPolyLines
{
public static ObservableCollection<Connections> connections = new ObservableCollection<Connections>();
}
public class Connections
{
public IEnumerable<IEnumerable<Point>> Points { get; set; }
public Connections(Point p1, Point p2)
{
Points = new List<List<Point>>
{
new List<Point>
{
p1, p2
}
};
}
}
And then a bunch of things like this:
GeoPolyLines.connections.Add(new Connections(new Point(GeoLocations.locations[0].Longitude, GeoLocations.locations[0].Latitude), new Point(GeoLocations.locations[1].Longitude, GeoLocations.locations[1].Latitude)));
So GeoPolyLines.connections will eventually have a bunch of different locations that I want to then write out to a .txt file to save and reload if I need to. But I don't know how to do this. I have something like this:
using (StreamWriter sw = new StreamWriter(filename))
{
var enumerator = GeoPolyLines.connections.GetEnumerator();
while (enumerator.MoveNext())
{
}
sw.Close();
}
use serialization.
To write to file
var serializer = new JavaScriptSerializer();
File.WriteAllText(filename, serializer.Serialize(points));
and to read from file
var points = serializer.Deserialize<List<Point>>(File.ReadAllText(filename));
Related
Following this tutorial: http://www.codeproject.com/Articles/26528/C-Application-to-Watch-a-File-or-Directory-using-F
I've modified the above tutorial and created a windows form application that can create multiple filesystemwatcher instances and track/delete them in a list on the form.
My next goal is to make it so that on computer restart/startup, the windows form application will remain active as well as all the watcher objects I've created.
I figure that this would take 4 parts:
1) Save information on watcher objects and their metadata to be accessed after restart (currently I save references to all watcher objects in a List variable)
2) Have the watcher objects persist even after restart
3) Start the windows form on startup
4) Access the saved watcher object information so that it still appears on the windows form list.
I am new to C# and this is my first project, so I have little idea on how to tackle these steps. Any help would be appreciated.
You have to create new watcher objects after restart. The information you need to store needs to have a different class, e.g.:
public class SettingItem
{
public string Path { get; set; }
}
public class Settings
{
public SettingItem[] Items { get; set; }
}
Then you can use XmlSerializer to store the objects on disk:
public static class SettingManager
{
public static void Save(string path, object obj)
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentNullException(nameof(path));
if (obj == null)
throw new ArgumentNullException(nameof(obj));
Directory.CreateDirectory(Path.GetDirectoryName(path));
using (var s = new StreamWriter(path))
{
var xmlSerializer = new XmlSerializer(obj.GetType());
xmlSerializer.Serialize(s, obj);
}
}
public static T Load<T>(string path) where T : class, new()
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentNullException(nameof(path));
if (File.Exists(path))
{
using (var s = new StreamReader(path))
{
var xmlSerializer = new XmlSerializer(typeof(T));
return xmlSerializer.Deserialize(s) as T;
}
}
return new T();
}
}
Usage:
var settingPath = #"c:\...xml";
var settings0 = new Settings
{
Items = new[]
{
new SettingItem { Path = #"c:\path\to\watch" },
}
};
SettingManager.Save(settingPath, settings0);
var settings1 = SettingManager.Load<Settings>(settingPath);
i want to deserialize an xml file and copy those values into csv file.i tried like this.
[Serializable, XmlRoot("Configuration"), XmlType("Configuration")]
public class LabelRequest
{
public string weightoz { get; set; }
public string MailClass { get; set; }
public static void DeSerialization()
{
LabelRequest label = new LabelRequest();
TextReader txtReader = new StreamReader(#"C:\xmlfile.xml");
XmlSerializer xmlSerializer = new XmlSerializer(typeof(LabelRequest));
label = (LabelRequest) xmlSerializer.Deserialize(txtReader);
txtReader.Close();
}
}
and the xml file is as follows
<Labelrequest>
<weightoz>2</weightoz>
<mailclass>abc</mailclass>
</labelrequest>
To write the values to a CSV file shouldn't be too difficult. Your example does not contain any code which writes to a file though. It only deserialises an XML file. Might I suggest something like this.
public static class LabelRequestSerializer
{
public static Label DeserializeXmlFile(string fileName)
{
using (TextReader txtReader = new StreamReader(fileName))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(LabelRequest));
LabelRequest label = (LabelRequest) xmlSerializer.Deserialize(txtReader);
}
}
public static void SerializeToCsv(LabelRequest labelRequest, string fileName)
{
if (labelRequest == null)
throw new ArgumentNullException("labelRequest");
StringBuilder sb = new StringBuilder();
sb.Append(labelRequest.weightoz);
sb.Append(",");
sb.Append(labelRequest.mailclass);
sb.AppendLine();
using (StreamWriter stream = new StreamWriter(fileName))
{
stream.Write(sb.ToString());
}
}
}
Then you can pass the instance of LabelRequest you want to serialize to these static methods. That way LabelRequest does not know about how to serialize itself from files, which is a nice seperation of concerns. Like this
void SomeMethod()
{
LabelRequest labelRequest = new LabelRequest();
LabelRequestSerializer.SerializeToCsv(labelRequest, #"C:\Path\Goes\Here\label.csv");
}
Edit...
If you really don't want to write out every property manually you can use reflection. However there will be a performance hit by using this. Shouldn't be a problem compared to the file IO though.
public static void SerializeToCsv(LabelRequest labelRequest, string fileName)
{
if (labelRequest == null)
throw new ArgumentNullException("labelRequest");
StringBuilder sb = new StringBuilder();
foreach (PropertyInfo info in labelRequest.GetType() .GetProperties())
{
object value = info.GetValue(labelRequest, null);
sb.Append(value);
sb.Append(", ");
}
sb.AppendLine();
using (StreamWriter stream = new StreamWriter(fileName))
{
stream.Write(sb.ToString());
}
}
I'm having a problem with the c# XML serialization system it's throws an Ambigus exception,
There was an error generating the XML document.
Now i have a Class that contains refferences to other classes and arrays of the other classes
E.G
namespace P2PFileLayout
{
public class p2pfile
{
public FileList FileList;
public StatusServer StatusServer;
public String Hash;
}
}
namespace P2PFileLayout.parts
{
public class StatusServer
{
public Auth Auth;
public Servers Servers;
}
public class Servers
{
public Server[] Server;
}
public class Server
{
public bool AuthRequired = false;
public string Address;
}
public class Files
{
public File[] File;
}
public class File
{
public string FileName = "";
public int BlockSize = 0;
public int BlockCount = 0;
}
public class Directory
{
public string Name;
public Files Files;
public Directory[] Dir;
}
public class Auth
{
public AuthServer[] AuthServer;
}
public class FileList
{
public Files Files;
public Directory[] Directory;
}
}
My Example data
// create the test file
testFile = new p2pfile();
// create a fake fileList
testFile.FileList = new P2PFileLayout.parts.FileList();
testFile.FileList.Directory = new P2PFileLayout.parts.Directory[1];
testFile.FileList.Directory[0] = new P2PFileLayout.parts.Directory();
testFile.FileList.Directory[0].Name = "testFolder";
testFile.FileList.Directory[0].Files = new P2PFileLayout.parts.Files();
testFile.FileList.Directory[0].Files.File = new P2PFileLayout.parts.File[2];
testFile.FileList.Directory[0].Files.File[0] = new P2PFileLayout.parts.File();
testFile.FileList.Directory[0].Files.File[0].FileName = "test.txt";
testFile.FileList.Directory[0].Files.File[0].BlockSize = 64;
testFile.FileList.Directory[0].Files.File[0].BlockCount = 1;
testFile.FileList.Directory[0].Files.File[1] = new P2PFileLayout.parts.File();
testFile.FileList.Directory[0].Files.File[1].FileName = "test2.txt";
testFile.FileList.Directory[0].Files.File[1].BlockSize = 64;
testFile.FileList.Directory[0].Files.File[1].BlockCount = 1;
// create a fake status server
testFile.StatusServer = new P2PFileLayout.parts.StatusServer();
testFile.StatusServer.Servers = new P2PFileLayout.parts.Servers();
testFile.StatusServer.Servers.Server = new P2PFileLayout.parts.Server[1];
testFile.StatusServer.Servers.Server[0] = new P2PFileLayout.parts.Server();
testFile.StatusServer.Servers.Server[0].Address = "http://localhost:8088/list.php";
// create a file hash (real)
HashGenerator.P2PHash hashGen = new HashGenerator.P2PHash();
testFile.Hash = hashGen.getHash();
treeView1.Nodes.Add(new TreeNode("Loading..."));
Classes.CreateTreeView ctv = new Classes.CreateTreeView();
ctv.BuildTreeView(testFile.FileList, treeView1);
treeView1.AfterCheck += new TreeViewEventHandler(treeView1_AfterCheck);
Now that is not as complicated as mine in terms of dept as my i loop objects so dir has support for more dirs but thats just an example
Then i'm serializing to a string var as i want to do a little more than just serialize it but here is my serialization
private string ToXml(object Obj, System.Type ObjType)
{
// instansiate the xml serializer object
XmlSerializer ser = new XmlSerializer(ObjType);
// create a memory stream for XMLTextWriter to use
MemoryStream memStream = new MemoryStream();
// create an XML writer using our memory stream
XmlTextWriter xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
// write the object though the XML serializer method using the W3C namespaces
ser.Serialize(xmlWriter, Obj);
// close the XMLWriter
xmlWriter.Close();
// close the memoryStream
memStream.Close();
// get the string from the memory Stream
string xml = Encoding.UTF8.GetString(memStream.GetBuffer());
// remove the stuff before the xml code we care about
xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
// clear any thing at the end of the elements we care about
xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
// return the XML string
return xml;
}
Can any one see why this is not working or any clues as to why it would not work normally
Why are you doing it manually?
What about this approach?
http://support.microsoft.com/kb/815813
Test test = new Test() { Test1 = "1", Test2 = "3" };
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(test.GetType());
MemoryStream ms = new MemoryStream();
x.Serialize(ms, test);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string xml = sr.ReadToEnd();
As for the ambigous message, take a look into the inner exceptions. XML serialization errors use innerexception a lot, and you usually have to look to all levels of InnerExceptions to know what is really happening.
I'm trying to use Protobuf-net to save and load data to disk but got stuck.
I have a portfolio of assets that I need to process, and I want to be able to do that as fast as possible. I can already read from a CSV but it would be faster to use a binary file, so I'm looking into Protobuf-Net.
I can't fit all assets into memory so I want to stream them, not load them all into memory.
So what I need to do is expose a large set of records as an IEnumerable. Is this possible with Protobuf-Net? I've tried a couple of things but haven't been able to get it running.
Serializing seems to work, but I haven't been able to read them back in again, I get 0 assets back. Could someone point me in the right direction please? Looked at the methods in the Serializer class but can't find any that covers this case. I this use-case supported by Protobuf-net? I'm using V2 by the way.
Thanks in advance,
Gert-Jan
Here's some sample code I tried:
public partial class MainWindow : Window {
// Generate x Assets
IEnumerable<Asset> GenerateAssets(int Count) {
var rnd = new Random();
for (int i = 1; i < Count; i++) {
yield return new Asset {
ID = i,
EAD = i * 12345,
LGD = (float)rnd.NextDouble(),
PD = (float)rnd.NextDouble()
};
}
}
// write assets to file
private void Write(string path, IEnumerable<Asset> assets){
using (var file = File.Create(path)) {
Serializer.Serialize<IEnumerable<Asset>>(file, assets);
}
}
// read assets from file
IEnumerable<Asset> Read(string path) {
using (var file = File.OpenRead(path)) {
return Serializer.DeserializeItems<Asset>(file, PrefixStyle.None, -1);
}
}
// try it
private void Test() {
Write("Data.bin", GenerateAssets(100)); // this creates a file with binary gibberish that I assume are the assets
var x = Read("Data.bin");
MessageBox.Show(x.Count().ToString()); // returns 0 instead of 100
}
public MainWindow() {
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e) {
Test();
}
}
[ProtoContract]
class Asset {
[ProtoMember(1)]
public int ID { get; set; }
[ProtoMember(2)]
public double EAD { get; set; }
[ProtoMember(3)]
public float LGD { get; set; }
[ProtoMember(4)]
public float PD { get; set; }
}
figured it out. To deserialize use PrefixBase.Base128 wich apparently is the default.
Now it works like a charm!
GJ
using (var file = File.Create("Data.bin")) {
Serializer.Serialize<IEnumerable<Asset>>(file, Generate(10));
}
using (var file = File.OpenRead("Data.bin")) {
var ps = Serializer.DeserializeItems<Asset>(file, PrefixStyle.Base128, 1);
int i = ps.Count(); // got them all back :-)
}
I have this c# class:
public class Test
{
public Test() { }
public IList<int> list = new List<int>();
}
Then I have this code:
Test t = new Test();
t.list.Add(1);
t.list.Add(2);
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
StringWriter sw = new StringWriter();
XmlSerializer xml = new XmlSerializer(t.GetType());
xml.Serialize(sw, t);
When I look at the output from sw, its this:
<?xml version="1.0" encoding="utf-16"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
the values 1,2 I added to the list member variable dont show up.
So how can I fix this ? I made the list a property but it still doesnt seem to work.
I am using xml serialization here, are there any other serializers ?
I want performance! Is this the best approach ?
--------------- UPDATE BELOW -------------------------
So the actual class I want to serialize is this:
public class RoutingResult
{
public float lengthInMeters { get; set; }
public float durationInSeconds { get; set; }
public string Name { get; set; }
public double travelTime
{
get
{
TimeSpan timeSpan = TimeSpan.FromSeconds(durationInSeconds);
return timeSpan.TotalMinutes;
}
}
public float totalWalkingDistance
{
get
{
float totalWalkingLengthInMeters = 0;
foreach (RoutingLeg leg in Legs)
{
if (leg.type == RoutingLeg.TransportType.Walk)
{
totalWalkingLengthInMeters += leg.lengthInMeters;
}
}
return (float)(totalWalkingLengthInMeters / 1000);
}
}
public IList<RoutingLeg> Legs { get; set; } // this is a property! isnit it?
public IList<int> test{get;set;} // test ...
public RoutingResult()
{
Legs = new List<RoutingLeg>();
test = new List<int>(); //test
test.Add(1);
test.Add(2);
Name = new Random().Next().ToString(); // for test
}
}
But the XML produced by the serializer is this:
<RoutingResult>
<lengthInMeters>9800.118</lengthInMeters>
<durationInSeconds>1440</durationInSeconds>
<Name>630104750</Name>
</RoutingResult>
???
its ignoring both of those lists ?
1) Your list is a field, not a property, and the XmlSerializer will only work with properties, try this:
public class Test
{
public Test() { IntList = new List<int>() }
public IList<int> IntList { get; set; }
}
2) There are other Serialiation options, Binary the main other one, though there is one for JSON as well.
3) Binary is probably the most performant way, since it is typically a straight memory dump, and the output file will be the smallest.
list is not a Property. Change it to a publicly visible property, and it should be picked up.
I figured it out that XmlSerializer doesnt work if I use IList so I changed it to List, that made it work. As Nate also mentioned.