I get this error:
The error description is 'Only one top level element is allowed in an XML document.'.
Could not find prepared statement with handle 0.
The XML parse error 0xc00ce555 occurred on line number 1, near the XML text "<value1>34</value1><value1>33</value1><value1>32</value1>".
The statement has been terminated.
This is Stored Procedure call:
public bool HideFromList(string commentList, bool state)
{
//commentList =<values><value1>34</value1><value1>33</value1><value1>32/value1></values>
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("VisibleFromList", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@XMLDoc", SqlDbType.Xml).Value = commentList;
cmd.Parameters.Add("@state", SqlDbType.Int).Value = state;
cn.Open();
int ret = cmd.ExecuteNonQuery();
return (ret == 1);
}
}
This is my Stored Procedure:
ALTER PROCEDURE dbo.VisibleFromList
(
@XMLDoc xml,
@state BIT
)
AS
BEGIN
DECLARE @docHandle int
EXEC sp_xml_preparedocument @docHandle OUTPUT, @XMLDoc
UPDATE tbh_Comments
SET Visible = @state WHERE
CommentID IN (SELECT * FROM OPENXML(@docHandle, '/values/value1', 2) WITH (value1 INT '.'))
END
But If I Modify SP with embedded input string it does work:
ALTER PROCEDURE dbo.VisibleFromList
(
@XMLDoc xml,
@state BIT
)
AS
BEGIN
DECLARE @docHandle int
EXEC sp_xml_preparedocument @docHandle OUTPUT,
'<values>
<value1>33</value1>
<value1>34</value1>
</values>'
UPDATE tbh_Comments
SET Visible = @state WHERE
CommentID IN (SELECT * FROM OPENXML(@docHandle, '/values/value1', 2) WITH (value1 INT '.'))
END
How to make it work with input parameter?