0

I have a method to return as instance of a class to XML. Unfortunately, no XML is being returned.

Below is an example of trying to convert a simple Country Class to XML:

Country Class

public class Country : XmlMethods<Country>
{
    public readonly int Id;
    public readonly string Code;
    public readonly string Name;

private Country()
{
}

private Country(int id, string code, string name)
{
    Id = id;
    Code = code;
    Name = name;
}

public static Country Load(int countryId)
{
    DataRow dr = //Row From Database

    return new Country(
            (int)dr["ISOCountryID"],
            (string)dr["ISOCode"],
            (string)dr["ISOCountry"]);
}

Inhertited Class

public class XmlMethods<T> : BaseClass
{
    public XElement ToXElement()
    {
        using (var memoryStream = new MemoryStream())
        {
            using (TextWriter streamWriter = new StreamWriter(memoryStream))
            {
                var xmlSerializer = new XmlSerializer(typeof (T));
                xmlSerializer.Serialize(streamWriter, this);
                return XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray()));
            }
        }
    }
}

Test Method

public void LoadCountry()
{
    lbl.Text = "***" + Country.Load(1).ToXElement().ToString() + "***";
}

There are no errors at runtime. All I get from my label is '******'

Any help would be greatly appreciated. Thanks.

1

3 Answers 3

1

Try to change this ... i removed some of your code to make this work for me so its a template of what to do:

public class Country : XmlMethods<Country>
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }

    private Country()
    {
    }

    private Country(int id, string code, string name)
    {
        Id = id;
        Code = code;
        Name = name;
    }

    public static Country Load(int countryId)
    {
        //test implementation
        return new Country(
                1,
                "some",
                "someother");
    }
}

public class XmlMethods<T>
{
    public XElement ToXElement()
    {
        StringBuilder xml = new StringBuilder();

        var xmlSerializer = new XmlSerializer(typeof(T));
        xmlSerializer.Serialize(new StringWriter(xml), this);
        return XElement.Parse(xml.ToString());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that your properties are readonly, and XmlSerializer isn't able to deal with that.

I would follow this answer in order to achieve the same results that you're looking for.

I think the [DataSerializer] method would probably help you the best. You'll just have to change your properties to the following:

[DataSerializer]
public int Id { get; private set; }

This way, they're still "readonly", but you can serialize them that way.

Comments

0

I would ditch the MemoryStream and the StreamWriter and use https://msdn.microsoft.com/en-us/library/system.xml.linq.xcontainer.createwriter%28v=vs.110%29.aspx e.g.

public XElement ToXElement()
{
    XDocument doc = new XDocument();
    using (XmlWriter xw = doc.CreateWriter())
    {

            var xmlSerializer = new XmlSerializer(typeof (T));
            xmlSerializer.Serialize(xw, this);
    }

    return doc.Root;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.