SqlDataReader不生成XML文档

时间:2010-06-30 00:08:46

标签: c# .net xml

我正在尝试为web服务动态创建XML,但是当我测试服务时,我收到以下错误

XML解析错误:未找到任何元素 地点:http://stuiis.cms.gre.ac.uk/dd615/aspweb/WatCoursework/Service.asmx/getMusicdetailsSql 第1行,第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 个答案:

答案 0 :(得分:3)

我想你忘了将musicInformation添加到文档中:

        }
        doc.AppendChild(musicInformation);
        return doc;
    }