0
<root>
  <source>
    <fields>
      <string propertyName="Status" class="System.String" nullable="true" valueInputType="all" displayName="status" description="Condition" maxLength="150" />
      
      <function methodName="Calculator" displayName="Calculator" includeInCalculations="true">
        <parameters>
          <input valueInputType="all" class="System.String" type="string" nullable="true" maxLength="256" />
        </parameters>
        <returns valueInputType="all" class="System.Double" type="numeric" nullable="false" min="-9007199254740992" max="9007199254740992" allowDecimal="true" allowCalculation="true" />
      </function>
    </fields>
  </source>
</root>

I have to generate an object which can resemble this XML like this:

[Field(DisplayName = "Status", Max = 150, Description = "Condition")]
        public string Status;
[Method(DisplayName = "Calculator")]
        public double Calculator(string st)
        {
            double num = st.length();
            return num;
        }

I may not even use the object directly even if I receive the type of Object is fine.

Akhil
  • 31
  • 6

1 Answers1

0

Try something like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication11
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            var fields = GetFields(doc);
        }
        static object GetFields(XDocument doc)
        {
            var results = doc.Descendants("fields").Select(x => new
            {
                Status = (string)x.Element("string").Attribute("propertyName"),
                Max = (int)x.Element("string").Attribute("maxLength"),
                Description = (string)x.Element("string").Attribute("description"),
                Calculator = (double?)x.Element("returns")

            }).ToList();

            return results;
        }
    }
 

}
jdweng
  • 33,250
  • 2
  • 15
  • 20