For example:
private System.Collections.Generic.List
this.People = new System.Collections.Generic.List
What I'd like to do is somehow export these lists out..don't mind what format..flatfile..whatever..
And then be able to import them in "recreating" the list (object?)
Any direction would be appriciated.
Thanks,
VulpesPosted May 9, 2013, 4:11 PM
However, as you suggest yourself, you can get around this by using an XmlTextReader whose Normalization property is set (by default) to false. The one which the Deserialize method uses under the hood has its Normalization property set to true.
Here's my original example changed to use an XmlTextReader which is slightly simpler than what you already have:
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
public class Person
{
public string Name {get; set;}
}
class Test
{
static void Main()
{
var people = new List
var p = new Person();
p.Name = "Jay\0";
people.Add(p);
var p2 = new Person();
p2.Name = "Vulpes";
people.Add(p2);
var xs = new XmlSerializer(typeof(List
// serialize to disk
using (Stream s = File.Create("Persons.xml"))
{
xs.Serialize(s, people);
}
// deserialize to a new list
List
using(Stream s = File.OpenRead("Persons.xml"))
{
using(XmlTextReader reader = new XmlTextReader(s))
{
people2 = (List
}
}
// check it worked
foreach(Person pers in people2) Console.WriteLine(pers.Name);
Console.ReadKey();
}
}
Jay SPosted May 10, 2013, 8:37 AM
Successfully serialized my list/objects and deserialized them on another machine.
Jay SPosted May 9, 2013, 11:37 AM
There is an error in XML document (16, 24)
Inner Exception:
{"'.', hexadecimal value 0x00, is an invalid character. Line 16, position 24."}
I'm guessing its due to the fact I have a value like:
Italy\0n\0\0\0
And its seeing this as hex or something..
I'm not to sure how to get by this..maybe wrap it in a text reader!?
EDIT:
Maybe...
TextReader tr = null;
VulpesPosted May 9, 2013, 11:31 AM
You have to apply the attribute to each property or field to be ignored by the serializer.
Jay SPosted May 9, 2013, 10:33 AM
Does [XmlIgnore] only effect up to the ";" so will have to do for each one?
VulpesPosted May 9, 2013, 9:33 AM
Jay SPosted May 9, 2013, 7:53 AM
VulpesPosted May 9, 2013, 6:21 AM
They have no effect otherwise.
Jay SPosted May 9, 2013, 6:06 AM
[XmlIgnore]
public List
Goes in my list/object class and NOT the function which is doing the serialization?
VulpesPosted May 8, 2013, 2:12 PM
We have had problems in the past with members being spammed by folks who'd joined especially for that purpose - 'lonely women' and the like - so they're probably trying to stop that by imposing a minimum points requirement to send a message.
Anyway, the answer to your question is that you can prevent a public property or field from being serialized by decorating it with the [XmlIgnore] attribute. For example:
class MyClass
{
public int MyInt; // this will be serialized
[XmlIgnore]
public List
}
Jay SPosted May 8, 2013, 12:32 PM
Is there any way to NOT serialize those lists referenced in the list/object being serialized...so some sort of skip over?
Jay SPosted May 8, 2013, 4:24 AM
If there's no way around it I will just go via this...
VulpesPosted May 8, 2013, 4:22 AM
If it still doesn't work, I'll try sending you one and see if you can then reply to it.
However, I'm going out now for a few hours so it will be a while before I can reply to it in any case.
Jay SPosted May 8, 2013, 4:15 AM
VulpesPosted May 8, 2013, 4:10 AM
On the second point, 'people' should be replaced with whatever List
Jay SPosted May 8, 2013, 3:49 AM
Both a seperate lists in there own right serilization will serializes these as well and when importing them back in will re-create the as their own uinique lists?
Also..
using (Stream s = File.Create("Persons.xml"))
{
xs.Serialize(s, people);
}
What is "people" meant to be replaced with?
VulpesPosted May 7, 2013, 5:27 PM
So I'd expect it to all to work now.
Might be best though to serialize a list containing a single Person object, then deserialize it to make sure all layers of objects are being recreated correctly.
Jay SPosted May 7, 2013, 4:49 PM
Only concern is...
Because Person "calls"/"Uses" :
Both of these are lists in there own rights and the serialization popped these all in the XML as well so its almost like its combined everything up...
VulpesPosted May 7, 2013, 4:10 PM
public FooDate(){}
Do the same for Player and NonPlayer if needed, then try to serialize again.
Jay SPosted May 7, 2013, 4:07 PM
int, strings, shorts, bytes..
I also have:
public FooDate ClubContractStart;
Where FooDate is a class.
I also have:
VulpesPosted May 7, 2013, 3:57 PM
Can you list the types of your 32 properties please. You can ignore simple stuff such as int, long, double, bool, char, string etc.
Jay SPosted May 7, 2013, 3:52 PM
What does "no parameterless constructor" mean, i.e. how can I tell?
In my Person Class I already have:
VulpesPosted May 7, 2013, 3:48 PM
One reason why serialization often fails is because the type you're trying to serialize has no parameterless constructor which (for technical reasons) it needs to have.
If your Person class has a constructor which takes no parameters (or it has no constructor at all) then this can't be the reason.
However, if the only constructor which it does have takes parameters, then add this line to the Person class and try serializing again:
public Person(){}
Jay SPosted May 7, 2013, 3:37 PM
var xs = new XmlSerializer(typeof(List
With:
I don't actually have a list call List`1 which I find very odd?
VulpesPosted May 7, 2013, 3:27 PM
var xs = new XmlSerializer(typeof(List
// serialize to disk
using (Stream s = File.Create("Persons.xml"))
{
xs.Serialize(s, people);
}
// deserialize to a new list
List
using(Stream s = File.OpenRead("Persons.xml"))
{
people2 = (List
}
It really is quite simple compared to other approaches such as CSV files where you'd need to specify each property individually when writing the list elements to the file and reading them back.
Under the hood, the serializer figures out using reflection which properties you have and then writes their values to the XML file so you are relieved from doing this 'manually'.
Do you have any more information on the error which may give us a clue as to what's wrong?
Jay SPosted May 7, 2013, 3:03 PM
There was an error reflecting type 'System.Collections.Generic.List`1[Foo.Person]'.
I really do think this is way above my head...I don't even understand what each part is, what to replace them with from my code...completely confused!
VulpesPosted May 7, 2013, 2:34 PM
Also it doesn't matter how many elements there are in the List which you'll already have populated before serialization takes place.
It'll produce a large XML file, of course, but I'd try it and see if it works 'straight out of the box'.
There are a number of reasons why it may not work immediately but these are usually easily fixed by decorating some properties with suitable attributes.
Jay SPosted May 7, 2013, 2:04 PM
The class that initially creates the list in question?
In one object list I have 32 objects(not sure on terminology) per item and there may be 5000 items in the list ( so 5000 persons with 32 pieces of information attached to them)
Does this mean on
I'd have to manually write out each and every one?
One of my lists is like this (sorry for the doctering)
VulpesPosted May 7, 2013, 1:44 PM
Here's some sample code: