locked
LINQ to XSD Load() from String

    Question

  • Hi, I'm trying to use LINQ to XSD in a project to generate classes from a schema doc. So far it is very useful, but I am hitting a stopping point when trying to load a strongly typed object with the Xml from a string or stream. All the examples I have found show loading the object from the .Load() method and only accepts a system file path for an xml document. I've tried to use the Serializer to deserialize the Xml string to the generated XSD object, but get a namespace not expected error. If I save the xml string off to a file path, and then open that file with the .Load method then everything is fine. Can someone help me out with a code sample?

    I just need to populate a stronly typed generated object with an in memory xml string using either the built in Linq to XSD methods, or the Xml.Serialization method.


    Monday, July 14, 2008 6:12 PM

Answers

  • Found the answer. It does not seem possible to Load a string into the XSD generated class, but using Isolated Storage to save the Xml stream from the Http response and then calling Load from Isolated storage using the reflected path.

    Here's the code I used: Pulled from http://www.codeplex.com/googlecontactsapi

                var stream = ExecuteApiCallStream(parameterList, true);

                var fileName = "response.xml";

                var isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
                var oStream = new IsolatedStorageFileStream(fileName, FileMode.Create, isoStore);
                // Get the physical path using reflection
                var filePath = oStream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(oStream).ToString();

                var streamWriter = new StreamWriter(oStream);
                var streamReader = new StreamReader(stream);

                streamWriter.Write(streamReader.ReadToEnd());
                streamWriter.Flush();
                streamWriter.Close();
                streamReader.Close();

                return marketplace_getCategories_response.Load(filePath);
    Monday, July 14, 2008 8:30 PM