O
old_School
Guest
Trying to make a simple app to convert my c# object to XML. I have an API that takes in XML and sends out XML. I want to build the objects as c# objects and then convert them to XML. Its working fine no errors but the XML conversion is not correct or at least I don't think my object is correct. I used a tool online to convert my XML to an object but I don't think it built it correctly:
XML:
<FlexFacadeObject xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Columns>
<cst_key>861a06c6-f210-406c-a0c7-0002a1353d73</cst_key>
<allAHA_Primary>1</allAHA_Primary>
</Columns>
</FlexFacadeObject>
This is what I need to build in c#
object:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Object_To_XML
{
[XmlRoot(ElementName = "FlexFacadeObject")]
public class FlexFacadeObject
{
[XmlElement(ElementName = "Columns")]
public Columns Columns { get; set; }
[XmlAttribute(AttributeName = "i", Namespace = "A Namespace Name for xmlns Attributes")]
public string I { get; set; }
}
[XmlRoot(ElementName = "Columns")]
public class Columns
{
[XmlElement(ElementName = "cst_key")]
public string Cst_key { get; set; }
[XmlElement(ElementName = "allAHA_Primary")]
public string AllAHA_Primary { get; set; }
}
}
To me this looks wrong but not sure how to adjust it
Here is my code for the tool:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
namespace Object_To_XML
{
public partial class Form1 : Form
{
private FlexFacadeObject obj = new FlexFacadeObject();
public Form1()
{
InitializeComponent();
}
private void btnConvertObject_Click(object sender, EventArgs e)
{
string XML = GetXMLFromObject(obj);
MessageBox.Show(XML);
}
public static string GetXMLFromObject(object o)
{
StringWriter sw = new StringWriter();
XmlTextWriter tw = null;
try
{
XmlSerializer serializer = new XmlSerializer(o.GetType());
tw = new XmlTextWriter(sw);
serializer.Serialize(tw, o);
}
catch (Exception ex)
{
//Handle Exception Code
}
finally
{
sw.Close();
if (tw != null)
{
tw.Close();
}
}
return sw.ToString();
}
}
}
Here is the returned string:
<?xml version=\"1.0\" encoding=\"utf-16\"?><FlexFacadeObject xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" />
It should match the XML at the top but its not. So I assume my object is wrong.
Continue reading...
XML:
<FlexFacadeObject xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Columns>
<cst_key>861a06c6-f210-406c-a0c7-0002a1353d73</cst_key>
<allAHA_Primary>1</allAHA_Primary>
</Columns>
</FlexFacadeObject>
This is what I need to build in c#
object:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Object_To_XML
{
[XmlRoot(ElementName = "FlexFacadeObject")]
public class FlexFacadeObject
{
[XmlElement(ElementName = "Columns")]
public Columns Columns { get; set; }
[XmlAttribute(AttributeName = "i", Namespace = "A Namespace Name for xmlns Attributes")]
public string I { get; set; }
}
[XmlRoot(ElementName = "Columns")]
public class Columns
{
[XmlElement(ElementName = "cst_key")]
public string Cst_key { get; set; }
[XmlElement(ElementName = "allAHA_Primary")]
public string AllAHA_Primary { get; set; }
}
}
To me this looks wrong but not sure how to adjust it
Here is my code for the tool:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
namespace Object_To_XML
{
public partial class Form1 : Form
{
private FlexFacadeObject obj = new FlexFacadeObject();
public Form1()
{
InitializeComponent();
}
private void btnConvertObject_Click(object sender, EventArgs e)
{
string XML = GetXMLFromObject(obj);
MessageBox.Show(XML);
}
public static string GetXMLFromObject(object o)
{
StringWriter sw = new StringWriter();
XmlTextWriter tw = null;
try
{
XmlSerializer serializer = new XmlSerializer(o.GetType());
tw = new XmlTextWriter(sw);
serializer.Serialize(tw, o);
}
catch (Exception ex)
{
//Handle Exception Code
}
finally
{
sw.Close();
if (tw != null)
{
tw.Close();
}
}
return sw.ToString();
}
}
}
Here is the returned string:
<?xml version=\"1.0\" encoding=\"utf-16\"?><FlexFacadeObject xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" />
It should match the XML at the top but its not. So I assume my object is wrong.
Continue reading...