XML解析:第4行,第19个字符,text / xmldecl不在输入的开头

时间:2013-07-05 06:11:12

标签: c# asp.net xml

您好我已经使用了下面这段代码并将XML参数从C#传递给sql server我得到以下错误“XML解析:第4行,第19行,text / 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;
    }

1 个答案:

答案 0 :(得分: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());