DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Sunil Menon Guest

    Whats wrong with this code?


    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

  2. #2
    David Bayley Guest

    Re: Whats wrong with this code?

    Sunil Menon <sunil@itb-india.com> wrote in message
    news:3ca17c61$1@10.1.10.29...

    > // Serialize the purchase order, and close the TextWriter.
    > Stream xyz = new MemoryStream();
    > serializer.Serialize(xyz, po);


    I think you need to explicitly reposition the MemoryStream back to the
    beginning here with:

    xyz.Seek(0, SeekOrigin.Begin);

    > TextReader reader = new StreamReader(xyz);
    > XmlDocument doc = new XmlDocument();
    > file://Create a reader.
    > try
    > {
    > XmlTextReader xmreader = new XmlTextReader(reader);
    > while (xmreader.Read())
    > {
    > XmlNode a = doc.ReadNode(xmreader);
    > doc.AppendChild(a);
    > }


    The reader is already at the end of the stream, so no nodes are appended to
    the doc above.

    > doc.Save("Test.xml");


    This then raises the error, since the doc is empty (i.e. no root node).

    --
    David.




  3. #3
    Phil Weber Guest

    Re: Whats wrong with this code?

    > Can anyone tell me where I am wrong?

    Sunil: Well, you're in the wrong group, for starters. ;-) This is a VB.NET
    group. Questions about C# code should be posted to the csharp.general group.
    ---
    Phil Weber



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links