1

I have this existing XML file serving as a template with NO data, just simple nodes... here's a sample:

<?xml version="1.0" encoding="utf-8" ?>
<catalog>
  <cd>
    <title />
    <artist />
    <country />
    <company />
    <price />
    <year />
  </cd>
</catalog>

Now I have created a similar class for it.

public class Cd
    {
        public string Title { get; set; }
        public string Artist { get; set; }
        public string Country { get; set; }
        public string Company { get; set; }
        public string Price { get; set; }
        public string Year { get; set; }
    }

The purpose is this:

  1. Put values on the properties of the var cd = new Cd(); object
  2. Get the existing XML file (template) then pass the values in it (like mapping the object to the existing XML)
  3. Transform the XML template(with values) into XSLT to become HTML

I think that's all.

How to properly do this? Thanks a lot!

2 Answers 2

3

You can use serialization to achieve (1) and (2)

[Serializable]
    public class Cd
    {

        public string Title { get; set; }

        public string Artist { get; set; }       
        public string Country { get; set; }

        public string Company { get; set; }

        public string Price { get; set; }

        public string Year { get; set; }
    }

now in order to create an xml from an object use:

    public static string SerializeObject<T>(this T obj)
    {
        var ms = new MemoryStream();
        var xs = new XmlSerializer(obj.GetType());
        var xmlTextWriter = new XmlTextWriter(ms, Encoding.UTF8);
        xs.Serialize(xmlTextWriter, obj);
        string serializedObject = new UTF8Encoding().GetString((((MemoryStream)xmlTextWriter.BaseStream).ToArray()));
        return serializedObject;

    }

in order to create an object from XML use:

public static T DeserializeObject<T>(this string xml)
    {
        if (xml == null)
            throw new ArgumentNullException("xml");
        var xs = new XmlSerializer(typeof(T));
        var ms = new MemoryStream(new UTF8Encoding().GetBytes(xml));
        try
        {
            new XmlTextWriter(ms, Encoding.UTF8);
            return (T)xs.Deserialize(ms);
        }
        catch
        {
            return default(T);
        }
        finally
        {
            ms.Close();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I would create class:

class catalog
{
    public CD cd {get;set;}
}

Here is serialization and deserealization helper:

public class Xml
{
    public static string Serialize<T>(T value) where T : class
    {
        if (value == null)
        {
            return string.Empty;
        }
        var xmlSerializer = new XmlSerializer(typeof(T));
        var stringWriter = new StringWriter();
        using (var xmlWriter = XmlWriter.Create(stringWriter))
        {
            xmlSerializer.Serialize(xmlWriter, value);
            return stringWriter.ToString();
        }
    }

    public static T Deserialize<T>(string xml)
    {
        var serializer = new XmlSerializer(typeof(T));
        T result;

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

        return result;
    }
}

Simply call:

catalog catalogObject = Xml.Deserialize<catalog>(xmlCatalogString);

I suspect you also will need to put some attributes on properties like XmlElement(ElementName = "title") because it is case sensitive.

MSDN

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.