简体   繁体   中英

Serialized Class instance not returning XML

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.

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());
    }
}

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.

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 eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM