immtrac
asked on
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(stri ng pName)
{
param = pName;
}
}
this how I am using the object:
accData = new clsAccountDataTemplate();
accData.action = ActionType.ToString();
accData.account_name = provision.Element("Account Name").Val ue;
accData.email = provision.Ancestors("membe r").First( ).Attribut e("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
<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(stri
{
param = pName;
}
}
this how I am using the object:
accData = new clsAccountDataTemplate();
accData.action = ActionType.ToString();
accData.account_name = provision.Element("Account
accData.email = provision.Ancestors("membe
//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
ASKER
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 )
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 )
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(LicenseDataTemp late licenseDataTemplate)
{
license.Add(licenseDataTem plate);
}
public List<LicenseDataTemplate> license = new List<LicenseDataTemplate>(
And then:
public void AddLicense()
{
license.Add(new LicenseDataTemplate());
}
Or:
public void AddLicense(LicenseDataTemp
{
license.Add(licenseDataTem
}
ASKER
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("Account Name").Val ue;
accData.email = provision.Ancestors("membe r").First( ).Attribut e("email") .Value;
//Please help write code here to add license and parameters
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("Account
accData.email = provision.Ancestors("membe
//Please help write code here to add license and parameters
I assume that provision is XDocument or XElement...
You can use provision.Descendants("par ameters"). First() to get the XElement of "parameters"
for example:
XElement parameters = provision.Descendants("par ameters"). First()
Then get all parameters from the new XElement:
var listOfParameters = parameters.Descendants("pa rameter");
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???
You can use provision.Descendants("par
for example:
XElement parameters = provision.Descendants("par
Then get all parameters from the new XElement:
var listOfParameters = parameters.Descendants("pa
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???
ASKER
Yes. provision is License.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Looks like from your suggested solution I do not need following 2 methods, right?
public void AddLicense(LicenseDataTemp late license)
{
//this is where I am stuck
}
public void AddParameter(ParameterData Template parameter)
{
//this is where I am stuck
}
public void AddLicense(LicenseDataTemp
{
//this is where I am stuck
}
public void AddParameter(ParameterData
{
//this is where I am stuck
}
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.
However, if you make the Lists private then you will need to somehow expose a method that adds items to the lists.
ASKER
Thanks for you help.
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.