Matt Donnelly

Matt Donnelly

  • NA
  • 4
  • 1.8k

Having trouble with C# XmlSerializer

Apr 12 2015 2:07 PM
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("&quot;", " ");
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());








 

Answers (1)