vendredi 12 janvier 2018

Can i acheive this using c# and reflection with the correct format

i am trying to display data in xml using this format and using reflection to generate the data:

   <?xml version="1.0" encoding="utf-8"?>
   <Master>
   <object_one ref=1>
    <Reference_Number>1</Reference_Number>
    <Price>£2.25</Price>
    <Currency>USD</Currency>
    </object_one>

<object_two type = "List" object_Type="PayPal.Api.RelatedResources">
<Reference_Number>2</Reference_Number>
<Currency>GBP</Currency>
<Total>0.00</Total>
<Shipping_Method>POST</Shipping_Method>
<Sale type="object">
    <Reference_Number>3</Reference_Number>
    <Amount type="Object">
        <total>0.00</total>
        <currency>GBP</currency>

        <details>amountDetails</details>
        <subtotal>0.00</subtotal>
        <tax>0.00</tax>
        <shipping>0.00</shipping>
        <fee>0.00</fee>
        <handling_fee>0.00</handling_fee>
        <shipping_discount>20%</shipping_discount>
        <gift_wrap>String_type</gift_wrap>
    </Amount>
</Sale>

 <Amount type="Object" name="Amount">
    <Reference_Number>4</Reference_Number>
    <Collection_Method>DPD</Collection_Method>
    <Shipping_Method>DPD</Shipping_Method>
    <Tax>20.00%</Tax>

here is my c# code for generating this data and i cant seem to get the format right :

Reuse.cs

namespace Cmd { public class Reuse {

    public int Recursive_Count;
    public HashSet<Object> db_set = new HashSet<Object>();

    public XDocument sample = new XDocument(new XElement("Root_Level"));
    List<Object> list = new List<Object>();


    public XElement element;

    public void getCollections(IEnumerable<object> collection)
    {
        foreach (object get_temp in collection)
        {
            GetSingleObject(get_temp);
        }
    }


    public Reuse()
    {

    }


    public void GetSingleObject(object input)
    {
        if (input == null)
        {
            return;
        }
        else if (input is IEnumerable)
        {
            processCollection(((IEnumerable)input));
            return;
        }

        PropertyInfo[] type = input.GetType().GetProperties();
        foreach (var each in type)
        {
            Object currentValue = each.GetValue(input);
            var current_type = input.GetType();
            if (currentValue == null)
            {
                GetSingleObject(currentValue);
                var nul = each.GetValue(input);

                if (nul == null)
                    continue;
                else
                    db_set.Add(input);

                Console.WriteLine("Property: {0} : {1}, Type : {2}", each.Name, each.GetValue(input), input.GetType());
                this.Recursive_Count++;
                continue;
            }

            Type currentValueType = currentValue.GetType();
            if (currentValueType.IsPrimitive || currentValueType.Namespace == "System")
            {
                Console.WriteLine("Property: {0} : {1}, Type: {2}", each.Name, currentValue, input.GetType());

                this.sample.Root.Elements().ToList().Add(new XElement("Elements", list.Select(m => new XElement(each.Name.ToString(), currentValue))));
                db_set.Add(input);
                if (currentValue == null)
                    continue;
            }
            else
            {
                IEnumerable collection = currentValue as IEnumerable;
                if (collection == null)
                {
                    GetSingleObject(currentValue);
                    this.Recursive_Count++;
                }
                else
                {
                    processCollection(collection);
                }
            }
        }
    }


    public void processCollection(IEnumerable collection)
    {
        Type collection_type = collection.GetType();
        Console.WriteLine("Collection: {0} : {1}", "Collection", collection.GetType());
        this.sample.Root.Add(new XElement("Collection", new XAttribute("type", collection.GetType()), new XElement("Elements", collection)));

        foreach (object listItem in collection)
        {
            GetSingleObject(listItem);
            db_set.Add(listItem);
            this.sample.Elements().ToList().Add(new XElement("Elements", list.Select(m => new XElement("data", new XAttribute("value", m)))));
        }
    }
}

}

I am trying to get the below structure in my xml document, i am close to getting this but i have got lost in the abstraction of this project. can you please point me in the right direction, would be greatly appreciated. thanks





Aucun commentaire:

Enregistrer un commentaire