none
Using Reflection and Utility Class to generate XML RRS feed

  • Question

  • Hi,

    I am trying to implement a class that uses reflection to generate an xml string.  

    I created a C# class using the XSD tool and an XML schema document.

    The following is a simple XSD example that I was using to test:

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="CustomerInfo">
     <xs:complexType>
    <xs:sequence>
    <xs:element name="Name" type="xs:string"/>
    <xs:element name="Address" type="xs:string"/>
    <xs:element name="City" type="xs:string"/>
    <xs:element name="State" type="xs:string"/>
    <xs:element name="Zip" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    The following is the code for my XMLdocuent utility class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace XMLtools
    {
        public class XMLdocument
        {
            
            public string generateXML(string xmlClass, string FieldValuePairs)
            {
                Type myXML = Type.GetType(xmlClass);

                var myXMLObj = Activator.CreateInstance(myXML);
                //var myXMLObj = (XMLdocument)Activator.CreateInstance(myXML);

                string[] fieldValuePairs = FieldValuePairs.Split('|');

                foreach (string fvPair in fieldValuePairs)
                {
                    char[] fvDelim = {'~'};
                    string[] fv = fvPair.Split(fvDelim);
                    var myField = myXML.GetField(fv[0].ToString());
                    myField.SetValue(myXML, fv[1].ToString());
                }

                return myXML.ToString();
            }
        }
    }

    In addition, I added the c# stub that was generated by the XSD tool to my project (not included).

    When I run the test application (to follow) the GetField method returns a null value, and the following line throws a NullReferenceException.

    Following is the code for a console application that I wrote to test the class.  To use it you must pass in the name 
    of the xml class and a delimited string representing the required field name / field value pairs for the data.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using XMLtools;

    namespace TextXMLTools
    {
        class Program
        {
            static void Main(string[] args)
            {
                XMLdocument myXdoc = new XMLdocument();
                string myInfo = "Name~Stuart Little|Address~101 Inthewall Rd|City~Anytown|State~IN|Zip~12345";
                string xmlStr = myXdoc.generateXML("CustomerInfo", myInfo);

                Console.WriteLine("\n\r" + xmlStr);
            }
        }
    }

    If someone can provide help to fix this it will be most appreciated.

    Sincerely,
    Matthew Paisley
    Wednesday, August 19, 2015 8:48 PM

Answers

  • I have done some additional research and made some changes to the code.  It has not solved the problem but has introduced a different issue that warrants a new post.
    • Marked as answer by Matt.Paisley Sunday, August 23, 2015 7:00 PM
    Sunday, August 23, 2015 6:59 PM

All replies

  • I think that you should use myXMLObj instead of myXML in several places.

    Thursday, August 20, 2015 5:36 AM
  • I have done some additional research and made some changes to the code.  It has not solved the problem but has introduced a different issue that warrants a new post.
    • Marked as answer by Matt.Paisley Sunday, August 23, 2015 7:00 PM
    Sunday, August 23, 2015 6:59 PM