3

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?

1 Answer 1

3

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;}
}
Sign up to request clarification or add additional context in comments.

11 Comments

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 ?
Yes it you could do this YourCustomObject object = Serialization<YourCustomObject>.DeserializeFromXmlFile(yourFileNameOrPath); which would result in a new object of the data in the XML.
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!
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.
Thanks its reallya big help
|

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.