2
我試圖動態地創建一個web服務一個XML,但是當我測試我得到以下錯誤的服務SqlDataReader對象不產生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對1映射到XML元素,將列1對1映射到子元素是「SELECT ... FOR XML AUTO,ELEMENTS」的完美場景。看看http://msdn.microsoft.com/en-us/library/ms188273.aspx,看看你是否可以使用它,它可能會爲你節省很多C#端的代碼。 – 2010-06-30 00:19:43