Ditty
Ditty

Reputation: 529

Parsing XML into a C# object that needs to be returned to the client

How do I parse this XML doc into a C# object so that I can return the object as JSON or XML? Please help.

 <?xml version="1.0" encoding="UTF-8"?>
<token_attributes>
  <token>
    <name>first_name</name>
    <type>string</type>
    <display>First Name</display>
    <description>First Name of Employee</description>
  </token>
  <token>
    <name>last_name</name>
    <type>string</type>
    <display>Last Name</display>
    <description>Last Name of Employee</description>
  </token>
  <token>
    <name>Email</name>
    <type>string</type>
    <display>Customer Email</display>
    <description>The Email to contact customer</description>
  </token>
  <token>
    <name>received_date</name>
    <type>date</type>
    <display>Received Date</display>
    <description>Received Date</description>
  </token>
  <token>
    <name>phone_number</name>
    <type>string</type>
    <display>Customer Support Phone Number</display>
    <description>The phone number to contact customer support</description>
  </token>
  <token>
    <name>employer_name</name>
    <type>string</type>
    <display>Employer Name</display>
    <description>The name of the Employer</description>
  </token>
  <token>
    <name>employee_id</name>
    <type>number</type>
    <display>Employee ID</display>
    <description>The Id of an Employee</description>
  </token>
  <token>
    <name>URL</name>
    <type>string</type>
    <display>Employer URL</display>
    <description>Employer URL</description>
  </token>
  <token>
    <name>claim_information</name>
    <type>array</type>
    <display>Claim Details</display>
    <description>Claim Information</description>
    <tokens>
        <token>
            <name>claim_id</name>
            <type>number</type>
            <display>Claim ID</display>
            <description>The ID of the Claim</description>
        </token>
        <token>
            <name>amount_claimed</name>
            <type>number</type>
            <display>Claim amount</display>
            <description>The claim amount</description>
        </token>
        <token>
            <name>expense_name</name>
            <type>string</type>
            <display>Expense Name</display>
            <description>The name of the expenses</description>
        </token>
        <token>
            <name>claim_date</name>
            <type>date</type>
            <display>Claim Date</display>
            <description>The date that the claim occurred</description>
        </token>
         <token>
            <name>claim_status</name>
            <type>string</type>
            <display>Claim Status</display>
            <description>The status of the claim</description>
        </token>
         <token>
            <name>claim_status_reason</name>
            <type>string</type>
            <display>Claim Status Reason</display>
            <description>The reason for the claim status</description>
        </token>
      </tokens>
    </token>
  </token_attributes>

I created my C# object as follows:

public class TokenDetails
{

    public string Name { get; set; }

    public string Type { get; set; }

    public string Display { get; set; }

    public string Description { get; set; }

}


public class Tokens
{

    public string Name { get; set; }

    public string Type { get; set; }

    public string Display { get; set; }

    public string Description { get; set; }

    public List<TokenDetails> TokenDetailsRepeater { get; set; }


}

So far this is what I have:

    XmlDocument xml = new XmlDocument();
    xml.LoadXml(myXMLString); //suppose that myXMLString contains the above XML

    List<Tokens> templateTokens = new List<Tokens>();

    XmlNodeList xnList = xml.SelectNodes("/token_attributes/token");
    foreach (XmlNode xn in xnList)
    {
        string name = xn["name"].InnerText;
        string type = xn["type"].InnerText;
        string display = xn["display"].InnerText;
        string description = xn["description"].InnerText;

        Tokens templateToken = new Tokens();

        templateToken.Name = name;
        templateToken.Type = type;
        templateToken.Display = display;
        templateToken.Description = description;

        templateToken.TokenDetailsRepeater = new List<TokenDetails>();

        ....
        ....
        ....
        ....
        //This is where I am stuck.. I don't know how to read the child node Tokens
        //Is this the right way to parse the XML? Or is there any other easier way?
        //Also it is possible that the child node Tokens may or may not exist.
        //So how do I check for any null string or element? 
        // Please help... Thanks in advance.


        templateTokens.Add(templateToken);
      }

I don't know how to read the child node Tokens Also not sure if this is the right way to parse the above XML? Please help... Thanks in advance.

Upvotes: 0

Views: 222

Answers (1)

EZI
EZI

Reputation: 15364

Just declare this class

public class token
{
    public string name { get; set; }
    public string type { get; set; }
    public string display { get; set; }
    public string description { get; set; }
    public List<token> tokens { get; set; }
}

and then deserialize as

XmlSerializer ser = new XmlSerializer(typeof(List<token>),new XmlRootAttribute("token_attributes"));
var tokens = (List<token>)ser.Deserialize(File.OpenRead(filename));

Upvotes: 1

Related Questions