0

嗨,我使用了下面的代码并将 XML 参数从 C# 传递给 sql server 得到以下错误“XML 解析:第 4 行,字符 19,文本/xmldecl 不在输入的开头”

<?xml version="1.0"?>
<ArrayOfInt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <int>1</int>
 </ArrayOfInt>

这是我的 C# 代码:

 private void searchLeval1Language()
{

    List<int> ClientId = new List<int>();
    List<int> FieldId = new List<int>();
    List<int> StatusId = new List<int>();
    string[] Client = null;
    Client = hdnClientID.Value.Split(',');
    if (Client != null)
    {

        foreach (string s in Client)
        {
            if (!string.IsNullOrEmpty(s))
            {
                ClientId.Add(Convert.ToInt32(s));
            }
        }
    }
    string[] Field = null;
    Field = hdnFieldID.Value.Split(',');
    if (Field != null)
    {
        foreach (string s in Field)
        {
            if (!string.IsNullOrEmpty(s))
            {
                FieldId.Add(Convert.ToInt32(s));
            }
        }
    }
    XmlSerializer xs = new XmlSerializer(typeof(List<int>));
    MemoryStream ms = new MemoryStream();
    xs.Serialize(ms, ClientId);
    string xmlClientid = UTF8Encoding.UTF8.GetString(ms.ToArray());
    xs.Serialize(ms, FieldId);
    string xmlFieldid = UTF8Encoding.UTF8.GetString(ms.ToArray());
    xs.Serialize(ms, FieldId);
    string xmlStatusid = UTF8Encoding.UTF8.GetString(ms.ToArray());
    DataTable dtLangL2 = RetrievalProcedures.GetPartnerProfileIdforDashboardSearchLevel2(hdnL1toL2.Value, xmlClientid, xmlStatusid);
    chLanguageL2.Series["LangSeriesL2"].XValueMember = "FieldName";
    chLanguageL2.Series["LangSeriesL2"].YValueMembers = "cnt";
    chLanguageL2.DataSource = dtLangL2;
    chLanguageL2.DataBind();
}

这是将参数传递给数据库的代码

      public static DataTable GetPartnerProfileIdforDashboardSearchLevel2(System.String language, System.String clientId, System.String statusProId)
    {
        // create parameters
        SqlParameter[] parameters = new SqlParameter[3];
        parameters[0] = new SqlParameter("@Language", SqlDbType.VarChar, 100, ParameterDirection.Input, true, 0, 0, "",  DataRowVersion.Current, language);
        parameters[1] = new SqlParameter("@ClientId", SqlDbType.Xml, 2147483647, ParameterDirection.Input, true, 0, 0, "",  DataRowVersion.Current, clientId);
        parameters[2] = new SqlParameter("@StatusProId", SqlDbType.Xml, 2147483647, ParameterDirection.Input, true, 0, 0, "",  DataRowVersion.Current, statusProId);

        // Call the stored proc.
        DataTable toReturn = new DataTable("GetPartnerProfileIdforDashboardSearchLevel2");
        bool hasSucceeded = DbUtils.CallRetrievalStoredProcedure("[JobApplicationModule].[dbo].[sp_GetPartnerProfileIdforDashboardSearchLevel2]", parameters, toReturn, null);

        return toReturn;
    }
4

1 回答 1

0

问题在这里:

xs.Serialize(ms, ClientId);
string xmlClientid = UTF8Encoding.UTF8.GetString(ms.ToArray());
xs.Serialize(ms, FieldId);
string xmlFieldid = UTF8Encoding.UTF8.GetString(ms.ToArray());
xs.Serialize(ms, FieldId);
string xmlStatusid = UTF8Encoding.UTF8.GetString(ms.ToArray());

第一次调用Serialize将整个 XML 文档写入MemoryStream. 第二个调用还将完整的 XML 文档写入同一流 - 但它放在第一个之后。因此,第二次调用ToArray检索一个字节数组,其中包含两个完整的 XML 文档,一个接一个。

如果您必须重用相同的流,请考虑调用SetLength它以在使用之间截断现有内容:

xs.Serialize(ms, ClientId);
string xmlClientid = UTF8Encoding.UTF8.GetString(ms.ToArray());
ms.SetLength(0);
xs.Serialize(ms, FieldId);
string xmlFieldid = UTF8Encoding.UTF8.GetString(ms.ToArray());
ms.SetLength(0);
xs.Serialize(ms, FieldId);
string xmlStatusid = UTF8Encoding.UTF8.GetString(ms.ToArray());
于 2013-07-05T06:29:17.027 回答