2010-06-30 4 views
2

Je suis en train de créer dynamiquement un XML pour un webservice mais quand je teste le service que je reçois l'erreur suivanteSqlDataReader ne pas produire le document XML

XML Erreur d'analyse: aucun élément trouvé Lieu: http://stuiis.cms.gre.ac.uk/dd615/aspweb/WatCoursework/Service.asmx/getMusicdetailsSql ligne numéro 1, colonne 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; 
     } 
+0

En passant, ce genre de chose - mappage des lignes 1 à 1 aux éléments XML, et des colonnes 1 à 1 aux sous-éléments, est un scénario parfait pour 'SELECT ... FOR XML AUTO, ELEMENTS'. Jetez un oeil à http://msdn.microsoft.com/en-us/library/ms188273.aspx et voyez si vous pouvez l'utiliser, cela pourrait vous faire économiser un peu de code sur le côté C#. –

Répondre

3

Je pense que vous avez oublié d'ajouter le musicInformation au document:

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