-3

in C# how do I parse through, or serialize an xml like the one below, thanx:

<response>
<result action="proceed" id="19809" status="complete" />
</response>
Andris Mudiayi
  • 459
  • 2
  • 6
  • 21

1 Answers1

1

This will fit your need: Tutorial or Stackoverflow And as @L.B was saying, google is your biggest friend in this case.

Other solution:

  1. Create a xsd schema for your xml.
  2. Use xsd.exe to create the classes for it.
  3. Serialize using standard serialization.

This is when i paste my helper class into the project and serialize away.

    /// <summary>
/// Serialization helper
/// </summary>
public static class XmlSerializationHelper
{
    /// <summary>
    /// Deserializes an instance of T from the stringXml
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="xmlContents"></param>
    /// <returns></returns>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
    public static T Deserialize<T>(string xmlContents)
    {            
        // Create a serializer
        using (StringReader s = new StringReader(xmlContents))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(s);
        }
    }

    /// <summary>
    /// Serializes the object of type T to the filePath
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="serializableObject"></param>
    /// <param name="filePath"></param>
    public static void Serialize<T>(T serializableObject, string filePath)
    {
        Serialize(serializableObject, filePath, null);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="serializableObject"></param>
    /// <param name="filePath"></param>
    /// <param name="encoding"></param>
    public static void Serialize<T>(T serializableObject, string filePath, Encoding encoding)
    {
        // Create a new file stream
        using (FileStream fs = File.OpenWrite(filePath))
        {
            // Truncate the stream in case it was an existing file
            fs.SetLength(0);

            TextWriter writer; 
            // Create a new writer
            if (encoding != null)
            {
                writer = new StreamWriter(fs, encoding);
            }
            else
            {
                writer = new StreamWriter(fs);
            }   

            // Serialize the object to the writer
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(writer, serializableObject);

            // Create writer
            writer.Close();
        }
    }
}
Community
  • 1
  • 1
Bob
  • 26
  • 2