0

I have got the following class generated from xsd.exe. Thats why i can't just add something like [XML Attribute("...")] to the 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;
        }
    }
}

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.

/// <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;
}

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:

Sample Xml Document

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:

 /// <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.");
 }

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.

/// <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;
}

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:

[XmlElement(ElementName = "MethodCheck")]
public partial class MethodCheckType { }

The error message is:

Attribute 'XmlElement' is not valid on this declaration type. Its only valid on 'property, indexer, field, param, return' declarations.

Could there be another using declaration i have to add? Or why this does not work?

FluepkeSchaeng
  • 1,441
  • 2
  • 11
  • 15

2 Answers2

1

Yes, you need to serialise (turn objects into some representation that can be stored, like a string), not deserialise (turn a string or some other representation into objects in memory).

The answer to your other question already gives you most of what you need.

public static string SerializeToXml(WebServiceType[] webServices)
{
    // Make a MethodCheck object to hold the services.
    // This ensures that you get a top-level <MethodCheck> tag in the XML.
    MethodCheckType container = new MethodCheckType();
    container.WebService = webServices;

    using (var writer = new StringWriter())
    {
        var serializer = new XmlSerializer(typeof(MethodCheckType));
        // Note that you're serializing, not deserializing.
        serializer.Serialize(writer, container);
        writer.Flush();
        return writer.ToString();
    }
}

However, there are two things to be careful of here:

  • Comparing two XML strings might not give you the results you want. Even if the XML is technically identical, even the smallest difference in insignificant whitespace will cause the string comparison to return false. For example, these two blocks of XML will fail a string comparison, even though the XML has the same structure:
<a><b>Text</b></a>

<a>
    <b>Text</b>
</a>
  • The names of the tags might not be right. The tags are called <MethodCheck> and <WebService>. The types are called MethodCheckType and WebServiceType, and they have no [XmlElement] attributes to give them different serialised names, which I think is what you referred to in the beginning of your answer. Because xsd.exe generates partial classes for you, you can create another source file that extends the generated classes. Example:
[XmlElement(ElementName = "WebService")]
partial class WebServiceType
{
}

// And the same for MethodCheckType.
Community
  • 1
  • 1
anton.burger
  • 5,637
  • 32
  • 48
0

Serialization and deserialization play a major role in Distributed architecture. To just make it simple you can say that serialization is transferring an object to string in a manner that it can be stored or transferred from one machine to another and vice versa process is deserialization.

dbc
  • 104,963
  • 20
  • 228
  • 340
Deepesh
  • 5,346
  • 6
  • 30
  • 45