Dear All,
I want to create a XML document by using MemoryStream but it gives me an
XMLException ( Error Message: The root element is missing.).
Can anyone tell me where I am wrong?
private void CreateXMLDoc(string filename)
{
// Create an instance of the XmlSerializer class;
// specify the type of object to serialize.
XmlSerializer serializer = new XmlSerializer(typeof(PurchaseOrder));
PurchaseOrder po=new PurchaseOrder();

// Create an address to ship and bill to.
Address billAddress = new Address();
billAddress.Name = "Rita Hernandez";
billAddress.Line1 = "1 Main St.";
billAddress.City = "AnyTown";
billAddress.State = "WA";
billAddress.Zip = "00000";
// Set ShipTo and BillTo to the same addressee.
po.ShipTo = billAddress;
po.OrderDate = System.DateTime.Now.ToLongDateString();

// Create an OrderedItem object.
OrderedItem i1 = new OrderedItem();
i1.ItemName = "Widget S";
i1.Description = "Small widget";
i1.UnitPrice = (decimal) 5.23;
i1.Quantity = 3;
i1.Calculate();

// Insert the item into the array.
OrderedItem [] items = {i1};
po.OrderedItems = items;
// Calculate the total cost.
decimal subTotal = new decimal();
foreach(OrderedItem oi in items)
{
subTotal += oi.LineTotal;
}
po.SubTotal = subTotal;
po.ShipCost = (decimal) 12.51;
po.TotalCost = po.SubTotal + po.ShipCost;
// Serialize the purchase order, and close the TextWriter.
Stream xyz = new MemoryStream();
serializer.Serialize(xyz, po);
TextReader reader = new StreamReader(xyz);
XmlDocument doc = new XmlDocument();
//Create a reader.
try
{
XmlTextReader xmreader = new XmlTextReader(reader);
while (xmreader.Read())
{
XmlNode a = doc.ReadNode(xmreader);
doc.AppendChild(a);
}
doc.Save("Test.xml");
}
catch(Exception e)
{
Console.WriteLine("Error Message: " + e.Message);
}
}

Thanks a lot in advance

Many Regards
Sunil