How to save a datatable if it contains an object that refuses to serialize?
I'm storing GData "Event" objects from the Google API in a datable (Ok, I had to cast them as an object or they wouldn't go in), and I can access all the properties of the events perfectly, and use them to update Google, but I cannot serialize the datatable (or a parent dataset) to fi开发者_如何学JAVAle because:
Type 'Google.GData.Calendar.EventEntry, Google.GData.Calendar, Version=1.4.0.2, Culture=neutral, PublicKeyToken=aa6748391206b888' does not implement IXmlSerializable interface therefore can not proceed with serialization.
This happens whether I try to simply serialize as XML or binary, and I can't find a way around the problem; can't any data structure be saved to a file one way or another?
To set the context, I was originally saving the GData EventFeed as XML which took about 30 seconds to figure out using Google API's myEventFeed.saveToXML, thinking I'd just reload it later... I have spent two days trying to get the xml back into an EventFeed object, and have now switched to saving the EventEntry from the EventFeed to a datatable instead.
So I managed to save the Feed to XML 1.0 easily, but reading it back into a GData EventFeed was a different story - Google give you myFeed.saveToXML but no loadfromXML method :-/ So I ended up trying to add System.Runtime.Serialization to use something like:
Dim reader As XmlReader = XmlReader.Create(fileName) Dim rssFeed As SyndicationFeed = SyndicationFeed.Load(reader)
This looked promising at first, but the data isn't all in rssFeed, plus I've still got to get the data back into a Google.GData.Calendar.EventFeed before I can call the methods I need, and I can't figure out how to do that either. I suppose could just copy the most important bits of data over from the rssFeed into new EventEntry's, or do the same thing with the datatable by extracting the important data... but that's going to be so inelegant, and risky down the line.
Any ideas which way I should turn now? Am I barking up the wrong trees?
Thanks, Neil
VB .NET 3.5 VS 2008
If you have an object that doesn't want to serialize, then there's no way to make it serialize.
This means you need to create an object that wants to serialize. Create an XML-Serializable class that will act as the proxy to the actual class. Your proxy will have a serializable property corresponding to each property of the original class that you want to see serialized:
public class Original
{
private string _property1;
private int _property2;
public string Property1
{
get { return _property1; }
}
public int Property2
{
get { return _property2; }
}
}
public class ProxyToOriginal
{
private readonly Original _original;
public ProxyToOriginal(Original original)
{
_original = original;
}
public string Property1
{
get { return _original.Property1; }
set { }
}
public int Property2
{
get { return _original.Property2; }
set { }
}
}
For anyone else who runs across this question, I think I've found the answer to the problem of serializing/deserializing the GData XML. I tried to do something similar to the OP but the EventFeed wouldn't serialize, so I used the SaveToXML method, but as the OP stated, there isn't a LoadFromXML method. I finally found the Parse() method, which works:
//first write the string to a file
List<EventEntry> eventList = new List<EventEntry>();
EventQuery query = new EventQuery(feed);
AtomFeed eventFeed = _service.Query(query) as AtomFeed;
XmlWriter writer = new XmlTextWriter("test.xml", new UTF8Encoding());
eventFeed.SaveToXml(writer);
writer.Close();
//next, parse the string from the file
Stream newFs = new FileStream("test.xml", FileMode.Open);
AtomFeed newFeed = new AtomFeed(new Uri("http://someURI"), null);
newFeed.Parse(newFs, AlternativeFormat.Atom);
newFs.Close();
精彩评论