0

So I have searched all I can, but cannot find the exact problem I am encountering.

This is my nested XML:

<Message>
    <Foo>
        <Bar>1</Bar>
        <Baz>2</Baz>
        <Qux>3</Qux>
    </Foo>
</Message>

And I have a class in C#:

[Serializable()]
[XmlRoot("Message")]
public class Foo
{
    [XmlElement("Bar")]
    public string Bar { get; set; }
    [XmlElement("Baz")]
    public string Baz { get; set; }
    [XmlElement("Qux")]
    public string Qux { get; set; }
}

Now Message is just arbitrary and gets sent with every XML message. So every XML message sent will have a <Message> tag around it. When I put Foo as the XmlRoot it throws an error, and with Message as XmlRoot it doesn't recognize the child elements. I am looking for a clean and easy solution to this. Thanks!

2 Answers 2

1

I haven't tested this, but it should work.

[Serializable()]
[XmlRoot("Message")]
public class Message
{
    [XmlElement("Foo")]
    public Foo Foo { get; set; }
}

[Serializable()]
public class Foo
{
    [XmlElement("Bar")]
    public string Bar { get; set; }
    [XmlElement("Baz")]
    public string Baz { get; set; }
    [XmlElement("Qux")]
    public string Qux { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have thought of this, and it would work except I would have to have an extra class for every class I want to Deserialize because message is used for every single XML message (and there are quite a few different ones). Or I would have to add every single class into the Message class as an XML element. Was hoping for a cleaner way, but this might be the only option. I will test it out. Thank you!
@j5juice This is the right (or clean) way to convert xml data into objects. There are no built-in way to avoid extra classes that I've heard of, However you can implement your own parser based on your specific schema.
Thank you. I will probably proceed this way. I am working through an issue when I deserialize that my Foo ends up empty, but I will accept this as the correct answer.
1

Use to get correct model here link

After that you can these methods to serialize and deserialize:

    public static class XMLFactory
{
    public static T XmlDeserializeFromString<T>(this string objectData)
    {
        return (T)XmlDeserializeFromString(objectData, typeof(T));
    }
    public static object XmlDeserializeFromString(this string objectData, Type type)
    {
        try
        {
            var serializer = new XmlSerializer(type);
            object result;

            using (TextReader reader = new StringReader(objectData))
            {
                result = serializer.Deserialize(reader);
            }

            return result;
        }
        catch (Exception ex)
        {
            LoggerHelper.LogError(ex.ToString());
            return null;
        }
    }
    public static string XmlSerializeToString(this object objectInstance)
    {
        var serializer = new XmlSerializer(objectInstance.GetType());
        var sb = new StringBuilder();

        using (TextWriter writer = new StringWriter(sb))
        {
            serializer.Serialize(writer, objectInstance);
        }

        return sb.ToString();
    }
}

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.