Serialize instace of autogenerated class to XML string

jeannepine

New Member
I have got the following class generated from xsd.exe. Thats why i can't just add something like \[code\][XML Attribute("...")]\[/code\] to the code. \[code\]public partial class MethodCheckType { private WebServiceType[] webServiceField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("WebService")] public WebServiceType[] WebService { get { return this.webServiceField; } set { this.webServiceField = value; } }}public partial class WebServiceType { private string uRLField; private string parameterField; private string returnValueField; private CredentialsType credentialsField; /// <remarks/> public string URL { get { return this.uRLField; } set { this.uRLField = value; } } /// <remarks/> public string Parameter { get { return this.parameterField; } set { this.parameterField = value; } } /// <remarks/> public string ReturnValue { get { return this.returnValueField; } set { this.returnValueField = value; } } /// <remarks/> public CredentialsType Credentials { get { return this.credentialsField; } set { this.credentialsField = value; } }}\[/code\]Maybe i have to change my XSD file and then i had to write this in the class code again.For understanding it better: I want to proof another method (the method "ReadXml" below) to work correct.\[code\]/// <summary>/// Reads an XML File in an array of WebServiceType objects./// </summary>/// <param name="path">The filename to read.</param>/// <returns>An array of WebServiceType Objects.</returns>public static WebServiceType[] ReadXml(string path){ // Is the path NOT a valic UNC path? if (!IsValidPath(path)) { Console.Write(MethodCheck.Properties.Resources.ERR003); return null; } XmlSerializer serializer = new XmlSerializer(typeof(MethodCheckType)); MethodCheckType output = null; StringReader reader = null; try { reader = new StringReader(path); output = (MethodCheckType)serializer.Deserialize(reader); } catch (Exception) { throw; } finally { reader.Dispose(); } return output.WebService;}\[/code\]To check the ReadXml method i have to write a method (for unti tests) which takes as params an array of WebServiceType objects an returns a string. I have no idea how to write this method. Below is a sample string:
HuuxK.jpg
Edit:This text seems to be hard to understand. I will try to formulate it in a clearer way:I already have got the ReadXml method. To proof whether it works correct or not i coded a test method:\[code\] /// <summary> ///A test for ReadXml ///</summary> [TestMethod()] public void ReadXmlTest2() { string path = @"C:\Users\pp-admin\Documents\Visual Studio 2010\Methodenpruefung der Webservices\Methodenpruefung\Methodenpruefung\BeispielXmlDatei.xml"; string expected = testXMLFile; string actual; WebServiceType[] xmlSerialized = WebserviceReader.ReadXml(path); // Deserialisieren des XML Objekts um einen String zu bekommen actual = WebServiceType.SerializeToXml(xmlSerialized); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }\[/code\]The method SerializeToXml has to take an array of WebServiceType objects, but it should return a complete XML string like its shown in the sample.\[code\]/// <summary>/// This method deserializes an arrayof WebServiceType objects into a XML string./// </summary>/// <param name="services">The WebServiceType object to deserialize.</param>/// <returns>A XML string.</returns>public static string SerializeToXml(WebServiceType[] services){ XmlSerializer serializer = new XmlSerializer(typeof(MethodCheckType)); MemoryStream ms = null; StreamReader reader = null; StringBuilder builder = new StringBuilder(); try { ms = new MemoryStream(); reader = new StreamReader(ms); Object t = (Object)serializer.Deserialize(reader); } finally { reader.Dispose(); ms.Dispose(); } return null;}\[/code\]Maybe on my side is some confusion what "serialization" and "deserialization" means, sorry about that. But i hope now it is a bit clearer what i exactly mean.Edit:First thanks to the answers below. The SerializeToXml Method seems to work now.There is another problem: With the following code i get an error:\[code\][XmlElement(ElementName = "MethodCheck")]public partial class MethodCheckType { }\[/code\]The error message is: \[quote\] Attribute 'XmlElement' is not valid on this declaration type. Its only valid on 'property, indexer, field, param, return' declarations.\[/quote\]Could there be another using declaration i have to add? Or why this does not work?
 
Back
Top