Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8247837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:59:29+00:00 2026-06-07T22:59:29+00:00

I have got the following class generated from xsd.exe. Thats why i can’t just

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:59:31+00:00Added an answer on June 7, 2026 at 10:59 pm

    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.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got the following class structure, and have plenty of classes like C
I've got a LINQ 2 SQL generated class I'd like to expose through a
I got multiple checkboxes generated from a PHP script. <input type=checkbox class=checkbox name=item[] value=1
I have got the following very simple code: function init() { var articleTabs =
I have got the following issue, my function appends code to a string $string
In some measure of progress since my last question I have got the following
I have got a the following problem: I have got multi-step form where in
I'm following the Django-CMS introductory tutorial and have got everything working up to the
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
Ive got the following problem. I have a model called user which has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.