2

I am trying to dynamically create a XML for a webservice but when I test the service I get the following error

XML Parsing Error: no element found Location: http://stuiis.cms.gre.ac.uk/dd615/aspweb/WatCoursework/Service.asmx/getMusicdetailsSql Line Number 1, Column 39: --------------------------------------^

// Make a new XML document in memory.
        XmlDocument doc = new XmlDocument();

        // Fill this document with a root element
        // named <Inventory>.
        XmlElement musicInformation = doc.CreateElement("musicInformation");

        using (SqlDataReader oDr = myCommand.ExecuteReader())
        {
            while (oDr.Read())
            {
                // Now, make a sub element named <Car> with
                // an ID attribute.
                XmlElement musicdetails = doc.CreateElement("musicdetails");
                musicdetails.SetAttribute("m_id", oDr["m_id"].ToString());

                // Build the data within the <Car> element.
                XmlElement p_id = doc.CreateElement("p_id");
                p_id.InnerText = oDr["p_id"].ToString();

                XmlElement artistname = doc.CreateElement("artistname");
                artistname.InnerText = oDr["artistname"].ToString();

                XmlElement recordname = doc.CreateElement("recordname");
                recordname.InnerText = oDr["recordname"].ToString();

                XmlElement recordtype = doc.CreateElement("recordtype");
                recordtype.InnerText = oDr["recordtype"].ToString();

                XmlElement format = doc.CreateElement("format");
                format.InnerText = oDr["format"].ToString();

                XmlElement price = doc.CreateElement("price");
                price.InnerText = oDr["price"].ToString();


                musicdetails.AppendChild(p_id);
                musicdetails.AppendChild(artistname);
                musicdetails.AppendChild(recordname);
                musicdetails.AppendChild(recordtype);
                musicdetails.AppendChild(format);
                musicdetails.AppendChild(price);

                musicInformation.AppendChild(musicdetails);
            }
            return doc;
        }
1
  • As a side note, this kind of thing - mapping rows 1-to-1 to XML elements, and columns 1-to-1 to subelements, is a perfect scenario for SELECT ... FOR XML AUTO, ELEMENTS. Have a look at msdn.microsoft.com/en-us/library/ms188273.aspx and see if you could use it, it might save you quite a bit of code on C# side. Commented Jun 30, 2010 at 0:19

1 Answer 1

3

I think you forgot to add the musicInformation to the document:

        }
        doc.AppendChild(musicInformation);
        return doc;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.