Ok so i have spend two days trying to work this out, i need my XML to be in the same format so that i can load in back though my form .
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfBike xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<bike>
<Make>Honda</Make>
<Model>CBR</Model>
<Year>2011</Year>
<Capacity>1000</Capacity>
</bike>
<bike>
<Make>Yamaha</Make>
<Model>YZ</Model>
<Year>2009</Year>
<Capacity>900</Capacity>
</bike>
<bike>
<Make>Suzuki</Make>
<Model>GB</Model>
<Year>2009</Year>
<Capacity>650</Capacity>
</bike>
</ArrayOfBike>
This is my code i have,
its for a windows form
XmlDocument doc = new XmlDocument();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create("C:\\Temp\\mybikes.xml",settings);
writer.WriteStartDocument();
writer.WriteStartElement("Bike");
foreach (string item in listBikes.Items)
{
string encodedXml = item.Replace(""", " ");
writer.WriteStartElement("Make");
writer.WriteStartElement("Model");
writer.WriteStartElement("Size");
writer.WriteStartElement("Year");
writer.WriteStartElement("Cost");
writer.WriteAttributeString("Bike", encodedXml);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
// Save the document to a file and auto-indent the output.
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(listBikes.GetType());
Having trouble with C# XmlSerializer
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Manoj BhoirRank #599 · 4K pointsPosted Apr 12, 2015, 9:05 PM EDT
Hi, you have not mentioned what problem exactly you are facing. There are many ways to create xml from serialize object. Please see test example for your requirement.
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.Serialization;
namespace XMLDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<bike> arrayBike = new List<bike>();
var bike1 = new bike();
bike1.Make = "Honda";
bike1.Model = "CBR";
bike1.Year = 2011;
bike1.Capacity = 1000;
arrayBike.Add(bike1);
var bike2 = new bike();
bike2.Make = "Yamaha";
bike2.Model = "YZ";
bike2.Year = 2009;
bike2.Capacity = 900;
arrayBike.Add(bike2);
textBox1.Text = GetXml(arrayBike);
}
public string GetXml<T>(T xmlObject)
{
var serializer = new XmlSerializer(typeof(T));
string utf8;
using (StringWriter writer = new Utf8StringWriter())
{
serializer.Serialize(writer, xmlObject);
utf8 = writer.ToString();
}
return utf8.ToString();
}
public class Utf8StringWriter : StringWriter
{
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}
}
}
public class bike
{
private string _make;
private string _model;
private int _year;
private int _capacity;
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public int Capacity { get; set; }
}
}
Please find attached source code of above sample.
I hope this will help you :)