Search code examples
c#xmlxsdxelement

C# - XElement from XSD or XML


I have to dialogue with a webservice whose response accepts a XElement object that represents a XML file. I have generated a XSD file from the XML and, via xsd.exe, I have generated a .cs class. What I want to know is: how I can convert the object from the .cs file into Xelement in order to provide it to the request? Is there a way to convert the XML to a XML object without declaring the nodes manually?

Best regards.


Solution

  • For example If you have a class.cs You should use Serialization

       static void Main(string[] args)
       {
          clsPerson p=new clsPerson();
          p.FirstName = "Jeff";
          p.MI = "A";
          p.LastName = "Price";
          System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
          x.Serialize(Console.Out, p);
          Console.WriteLine();
          Console.ReadLine();
       }