-1

I wrote a generic method for Object to XML string using XMLSerializer class. Below is my class

public class SampleJson
    {
        public string fname { get; set; }
        public string lname { get; set; }
        public int age { get; set; }
        public AdditionalInformation AdditionalInformation { get; set; }
    }

    public class AdditionalInformation
    {
        public string firstlane { get; set; }
        public string secondlane { get; set; }
        public decimal? cityCode { get; set; }
        public int? countryCode { get; set; }
        public bool? isValid { get; set; }
        public DateTime enteredDate { get; set; }
    }

And below is Generic Method

public class QAZ
    {
        public static string Foo<T>(T dataToSerialize)
        {
            var stringWriter = new StringWriter();
            XmlTextWriter xmlTextWriter = null;
            var serializer = new XmlSerializer(dataToSerialize.GetType());
            xmlTextWriter = new XmlTextWriter(stringWriter);
            serializer.Serialize(xmlTextWriter, dataToSerialize);
            return stringWriter.ToString();
        }
    }

class Bar
    {

        static void Main(string[] args)
        {


            var sampleJson = typeof(SampleJson);
            var fooMethod = typeof(QAZ).GetMethod("Foo");
            var fooOfBarMethod = fooMethod.MakeGenericMethod(new[] {sampleJson});
            string xml= fooOfBarMethod.Invoke(new QAZ(), new object[] {new SampleJson()}).ToString();
        Console.ReadKey();
        }
    }

But I'm getting the output is

<?xml version="1.0" encoding="UTF-16"?>

-<SampleJson xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<age>0</age>

</SampleJson>

I don't understand why XmlSerializer serializes only int property. Can anyone please tell me the issue that I'm doing.

The main agenda here is I want to generate an xsd for SampleJson class. In order to do that I am trying to convert the class to xml. From xml to xsd. Is there a way to generate xsd from a class?

dbc
  • 104,963
  • 20
  • 228
  • 340
Vijay
  • 745
  • 2
  • 9
  • 25
  • 1
    You didn't set any other property. All other properties are nulls. Why are you using reflection anyway? – Panagiotis Kanavos Oct 01 '19 at 08:59
  • 1
    What do you expect `new SampleJson()` to do? Set the properties to some magic value? Of course you have to set the values yourself. Otherwise they remain `null` for reference-types which serializer won´t handle. – MakePeaceGreatAgain Oct 01 '19 at 09:00
  • 4
    Serializer will ignore `null` values – Fabio Oct 01 '19 at 09:01
  • Why is this type named `sampleJson` anyway? This is XML, not JSON. What are you trying to do? Convert between JSON and XML? You can use Json.NET's [SerializeXMLNode and DeserializeXMLNode](https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm) to convert directly from JSON to XML and vice versa – Panagiotis Kanavos Oct 01 '19 at 09:10
  • Just now I gotto know that. The main agenda here is I want to generate an xsd for `SampleJson` class. Inorder to do that I am trying to convert the class to xml. From xml to xsd. Is there a way to generate xsd from a class? – Vijay Oct 01 '19 at 09:12
  • @Vijay - see [How to create a XSD schema from a class?](https://stackoverflow.com/q/10017139/3744182), [How to: Use the XML Schema Definition Tool to Generate Classes and XML Schema Documents](https://learn.microsoft.com/en-us/dotnet/standard/serialization/xml-schema-def-tool-gen). – dbc Oct 01 '19 at 15:16

1 Answers1

0

I think this might help you to understand better, I wrote an example please do like or dislike if it has or hasn't helped.

public class Vehicle
{
    public string VRM; 
}


class Program
{
    static void Main(string[] args)
    {

        var Reg = new Vehicle { VRM = "LP65 UGT" };
        var writer = new System.Xml.Serialization.XmlSerializer(typeof(Vehicle));
        var wfile = new System.IO.StreamWriter(@"C:\Myexample\NewVehicle.xml");
        writer.Serialize(wfile, Reg);
        wfile.Close();

        Console.WriteLine("Vehicle Reg is now written in xml format ");
        Console.ReadKey();






    }
}

The above example will write successfully to XML file ensure you create the folder first on your C drive My example and then run the above to see the result for yourself and see if it can point you to the correct direction hopefully.

Waheed Rafiq
  • 460
  • 1
  • 6
  • 17