How to create c# objects using xml

10,084

You will want to use some sort of de-serialization. Here is an example of one I implemented not too long ago:

public static class Serialization<T> where T : class   
{    

    public static T DeserializeFromXmlFile(string fileName)
    {
        if (!File.Exists(fileName))
        {
            return null;
        }

        DataContractSerializer deserializer = new DataContractSerializer(typeof(T));

        using (Stream stream = File.OpenRead(fileName))
        {
            return (T)deserializer.ReadObject(stream);
        }
    }
}

Then to call it you would do something like this:

Serialization<YourCustomObject>.DeserializeFromXmlFile(yourFileNameOrPath);

Remember that you would have to have a Class that corresponds to the XML you want to de-serialized. (aka turn into an object).

Your class would look something like this:

[Serializable]
class parameter
{
     [Datamember]
     public string name {get; set;}

     [Datamember]
     public string label {get; set;}

     [Datamember]
     public string unit {get; set;}

     [Datamember]
     public component thisComponent {get; set;}
}

[Serializable]
class component
{
    [Datamember]
    public string type {get; set;}

    [Datamember]
    public List<attribute> attributes  {get; set;}
}

[Serializable]
class attribute
{
    [Datamember]
    public string? type {get; set;}

    [Datamember]
    public string? displayed {get; set;}

    [Datamember]
    public string? add_remove {get; set;}

    [Datamember]
    public string? ccypair {get; set;}

    [Datamember]
    public List<int> item { get; set;}
}
Share:
10,084
Sss
Author by

Sss

Software developer using visual c++

Updated on June 04, 2022

Comments

  • Sss
    Sss almost 2 years

    I am new to C#, Silverlight 5 and XAML beginner. I am working on a VS-2012 project and I don't have to use any CycleClip Board Ring to do this task. I have an XML file in my VS project. Suppose the file is given below:

    FileName is FileXml.xml   
    
    <?xml version="1.0" encoding="utf-8" ?>
       <parameter>
       <name>mounts</name>
        <unit></unit>
          <component>
             <type>List</type>
             <attributes>
                <type>Integer</type>
                <displayed>4</displayed>
                <add_remove>yes</add_remove>
                <item>25</item>
             </attributes>
             <attributes>
                <ccypair>XAUUSD</ccypair>
                <item>100</item>
             </attributes>
          </component >
       </parameter>
    

    And I have to parse this XML file and have to create the object in C# .So that I would be able to use "bands_amounts" (name) and all other elements accessing through those objects. How to do this using C# code?

  • Sss
    Sss about 10 years
    So it will read the xml file. and YourCoustomObject is the resulted object ? Am i Right ? further i will do YourCoustomObject.name="Band_Amount"; Am i right ??? Could you please correct me if i am wrong ?
  • merp
    merp about 10 years
    Yes it you could do this YourCustomObject object = Serialization<YourCustomObject>.DeserializeFromXmlFile(yourF‌​ileNameOrPath); which would result in a new object of the data in the XML.
  • merp
    merp about 10 years
    Just remember that in order to de-serialize correctly, the properties in your class must be named the same as the elements you intend to extract from the XML. And yes, case does matter!
  • merp
    merp about 10 years
    I added some more code to the answer of what your class may look like. You should have a pretty good idea of where to go from there.
  • Sss
    Sss about 10 years
    Thanks its reallya big help
  • Sss
    Sss about 10 years
    and 1 more doubt Ohax how i will handle the "attributes" in my c# code ? because it contans a list.
  • Sss
    Sss about 10 years
    The type or namespace name 'Serializable' does not exist in the namespace 'System' (are you missing an assembly reference?) The type or namespace name 'SerializableAttribute' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'DatamemberAttribute' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'Datamember' could not be found (are you missing a using directive or an assembly reference?)
  • Sss
    Sss about 10 years
    I have tried to save the different classes (not all together) and i have following assemblies : using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Runtime.Serialization.Json; using System.Runtime; using System.Xml.Serialization; using System.Runtime.Serialization; using System.IO; using System.Xml.Serialization.XmlTypeAttribute;
  • Sss
    Sss about 10 years
    the problem is when i try to do Serialization<YourCustomObject>.DeserializeFromXmlFile(MyFil‌​eNameOrPath); in "attribute" class. Then will it work (in fact this line gives error)? what should be the alternative. And Serializable is not supported in silverlight. What should i use now ?
  • Sss
    Sss about 10 years
    wHY DO YOU HAVE "?" after string in attribute class.
  • merp
    merp about 10 years
    I'm sorry but I won't be able to keep answering questions like this. You need to do your own research, that's how you get better as a programmer. The "?" means that the string can be nullable. You can use that for any type.