Xml Raw data returned from SQL Server 2005 not correct when calling from .NET

梦想与她 提交于 2019-12-11 10:12:35

问题


I have a stored procedure that returns XML raw data FOR XML RAW something like the following format:

<row 
    codelistid="1" codelistname="LOCATION" 
    codeid="1557" codename="Hors Ile de France" languageid="1" />

When I run the stored procedure in Management Studio 1765 rows worth of data is returned but when I call the procedure from my C# code it appears to be around half of that 882. It seems that if two rows have the same codeid then only one is returned

I am using the xmlreader to return the XML row by row and then appending each row as an XElement to my results XDocument.

Here is how I am retrieving the data:

 using (SqlConnection conn = new SqlConnection(con))
        {
            XDocument results = new XDocument(
           new XElement("results"));

            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "MyStoredProc";
                conn.Open();
                var count = 0;
                using (XmlReader reader = cmd.ExecuteXmlReader())
                {

                    while (reader.Read())
                    {

                        results.Root.Add(XElement.Parse(reader.ReadOuterXml()));
                        count += 1;

                    }
                }

                return results;

            }
        }

If I change the SPROC to output the data rather than XML and read it via SQLDataReader it seems to work fine.

Anyone have any ideas on why this is happening as I would ideally like the database to return the XML?

Thanks in advance.


回答1:


reader.Read() and reader.ReadOuterXml() being in the same loop would skip one line. Try this :

using (XmlReader reader = cmd.ExecuteXmlReader())
            {
               reader.Read(); //For initial first read.
               while (!reader.EOF)
                {

                    results.Root.Add(XElement.Parse(reader.ReadOuterXml()));
                    count += 1;

                }
            }



回答2:


What about if you have the query data put into a DataSet, then write that data set to XML?

public GetXmlFromDataSet()
{
    DataSet myDataSet = new DataSet();
    DbDataAdapter myDataAdapter = DatabaseFactory.CreateDataAdapter();
    DbConnection DatabaseConnection = new DatabaseConnection(/*put appropriate values here*/);
    XmlTextWriter myXmlTextWriter = new XmlTextWriter(/*put appropriate values here*/)

    myDataDapter.SelectCommand = DatabaseFactory.CreateCommand();
    myDataDapter.SelectCommand.CommandType = CommandType.StoredProcedure;
    myDataAdapter.CommentText = "EXEC MyStoredProc";
    myDataAdapter.SelectCommand.Connection = DatabaseConnection;
    myDataAdapter.Fill(myDataSet);
    myDataSet.WriteXml(myXmlWriter);
}


来源:https://stackoverflow.com/questions/14899353/xml-raw-data-returned-from-sql-server-2005-not-correct-when-calling-from-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!