• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

How to write this C# class and call it as object

I have this xml

      <member email="dc@test.com">

            <provisions>

                  <provision>
                        <AccountName>
                              Test_Account                        
                        </AccountName>
                        <LicenseName>
                              Forms
                        </LicenseName>
                        <parameters>
                              <parameter>
                                    Resp_2
                              </parameter>
                              <parameter>
                                    Resp_3
                              </parameter>
                        </parameters>
                  </provision>


                  <provision>
                        <AccountName id="1234">
                              Test_Account_101
                        </AccountName>

                        <LicenseName>
                              Forms_101
                        </LicenseName>
                        <parameters>
                              <parameter>
                                    Resp_2_101
                              </parameter>
                              <parameter>
                                    Resp_3_101
                              </parameter>
                              <parameter>
                                    Resp_2_101
                              </parameter>
                              <parameter>
                                    Resp_2_101
                              </parameter>
                              <parameter>
                                    Resp_2_101
                              </parameter>
                        </parameters>
                  </provision>
</member>

I need to read this an transform to a C# object. I know how to read it but I need to implement method to add License and parameters. I want to set some propertiese and then want to add licenses and parameteres just by calling AddLicense and AddParameters (for the same license)



this is my class:

    public class clsAccountDataTemplate
    {
        public string account_name;
        public string account_id;
        public string email;
        public string old_email;
        public string ad_user_id;
        public string action;
        public LicenseDataTemplate[] license;
       
        public void AddLicense()
        {
           
            //this is where I am stuck
        }
    }

    public class LicenseDataTemplate
    {
        public string license_name;
        public ParameterDataTemplate[] parameters;

        public void AddParameter()
        {
                //this is where I am stuck
        }

    }

    public class ParameterDataTemplate
    {
        public string param;
        public ParameterDataTemplate(string pName)
        {
            param = pName;
        }

    }



this how I am using the object:

                        accData = new clsAccountDataTemplate();
                        accData.action = ActionType.ToString();
                        accData.account_name = provision.Element("AccountName").Value;
                        accData.email = provision.Ancestors("member").First().Attribute("email").Value;
                       //Need to add licencese and parameters for this object



but I am stuck to how to add licenses and then parameters to this object. I need some help
0
immtrac
Asked:
immtrac
  • 5
  • 5
1 Solution
 
saraganiCommented:
Hi, since your structure is XML, I would suggest you to use Serialization.
If you create your class then you will be able to save it easily to ab XML file using the .Net class of XML Serialization, and then load it using the same method.

This way, you don't need to implement your own load function.
However, you will need to change the structure of the XML file a little bit.
You will notice how it should look a like after you will save your class to an XML using serialization.
0
 
immtracAuthor Commented:
Thanks. But I don't have control over XML. I just need to know how to add license with parameters associated with that liscense to accData.license.

I need help to implement methods in class and then calling it to successfully add license (with parameters associated with that licenses) to accData.

I then just should be to get all proeritese from accData (email, account name, array of all the licenses and array of parameters for each license )
0
 
saraganiCommented:
Hi, since you are adding Licenses, and I assume that you can have more than one (you are using an array), then my best suggestion would be using List instead of array:

public List<LicenseDataTemplate> license = new List<LicenseDataTemplate>();


And then:

public void AddLicense()
        {
           
            license.Add(new LicenseDataTemplate());
        }



Or:

public void AddLicense(LicenseDataTemplate licenseDataTemplate)
        {
           
            license.Add(licenseDataTemplate);
        }


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
immtracAuthor Commented:
I am having difficulty understanding the concept.

Would you please explain with little code example. Like what should be in the class and how then I would use it in main code

                        accData = new clsAccountDataTemplate();
                        accData.action = ActionType.ToString();
                        accData.account_name = provision.Element("AccountName").Value;
                        accData.email = provision.Ancestors("member").First().Attribute("email").Value;
                       //Please help write code here to add license and parameters
0
 
saraganiCommented:
I assume that provision is XDocument or XElement...

You can use provision.Descendants("parameters").First() to get the XElement of "parameters"

for example:
XElement parameters = provision.Descendants("parameters").First()

Then get all parameters from the new XElement:
var listOfParameters = parameters.Descendants("parameter");

The iterate using "foreach" on listOfParameters and fill your parameters list on your license.




However, the XML is built in a way that Parameters are the children of "provision" and not of "LicenseName"

If you have only 1 license then don't have a list of licenses...
if the Parameters are supposed to be the children of the License then have the XML build this way.

Right now, you don't have any "License" on your XML, but LicenseName...
I would have expected:
<Licenses>
  <License Name="Form_101">
<parameters>
                              <parameter>
                                    Resp_2_101
                              </parameter>
                              <parameter>
                                    Resp_3_101
                              </parameter>
                              <parameter>
                                    Resp_2_101
                              </parameter>
                              <parameter>
                                    Resp_2_101
                              </parameter>
                              <parameter>
                                    Resp_2_101
                              </parameter>
                        </parameters>
  </License>
</Licenses>




Or maybe provision is License???
0
 
immtracAuthor Commented:
Yes. provision is License.
0
 
saraganiCommented:
First, you are missing </provisions>
Second account_name and account_id are members of clsAccountDataTemplate in your class, but are actually members of each provision in your XML!
And since you have several provisions then you don't have a single account_name and account_id



Here is a simple code that will help you continue with your task:

            XDocument doc = XDocument.Load(@"C:\test.xml");
            clsAccountDataTemplate accData = new clsAccountDataTemplate();
            XElement provisionsNode = doc.Descendants("provisions").First();
           
            accData.email = provisionsNode.Ancestors("member").First().Attribute("email").Value;

           var provisions = (from val in provisionsNode.Descendants("provision")
                             select val).ToList();

            foreach (XElement provision in provisions)
            {
                LicenseDataTemplate license = new LicenseDataTemplate();
                accData.Licenses.Add(license);
                license.license_name = provision.Descendants("LicenseName").First().Value;
                license.account_name = provision.Descendants("AccountName").First().Value;

                if (provision.Element("AccountName").Attribute("id") != null)
                {
                    license.account_id = provision.Element("AccountName").Attribute("id").Value;
                }

                var parameters = (from val in provision.Descendants("parameter")
                                  select val).ToList();

                foreach (XElement parameter in parameters)
                {
                    ParameterDataTemplate parameterDataTemplate = new ParameterDataTemplate(parameter.Value);
                    license.parameters.Add(parameterDataTemplate);
                }
            }






    public class clsAccountDataTemplate
    {
        public string email;
        public string old_email;
        public string ad_user_id;
        public string action;
        private List<LicenseDataTemplate> _licenses = new List<LicenseDataTemplate>();

        public void AddLicense(LicenseDataTemplate license)
        {
            //this is where I am stuck
        }

        public List<LicenseDataTemplate> Licenses
        {
            get
            {
                return _licenses;
            }
        }
    }

    public class LicenseDataTemplate
    {
        public string license_name;
        public string account_name;
        public string account_id;

        public List<ParameterDataTemplate> parameters = new List<ParameterDataTemplate>();

        public void AddParameter(ParameterDataTemplate parameter)
        {
            //this is where I am stuck
        }

    }

    public class ParameterDataTemplate
    {
        public string param;
        public ParameterDataTemplate(string pName)
        {
            param = pName;
        }

    }
0
 
immtracAuthor Commented:
Looks like from your suggested solution I do not need following 2 methods, right?

  public void AddLicense(LicenseDataTemplate license)
        {
            //this is where I am stuck
        }

        public void AddParameter(ParameterDataTemplate parameter)
        {
            //this is where I am stuck
        }
0
 
saraganiCommented:
We are working directly on the List, so yes, you can remove those functions.

However, if you make the Lists private then you will need to somehow expose a method that adds items to the lists.
0
 
immtracAuthor Commented:
Thanks for you help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now