Error while converting string xml to xml using xelement

I am making a string from dataset and then converting to xml.while converting its giving me error unexpected symbol “=” at location

For Each row In dsGetBVItemDetails.Tables(0).Rows
                       strxmlString += _
                           "<ItemDetails>" & _
                               "<Item code=" + dsGetBVItemDetails.Tables
(0).Rows(i)(0).ToString() + ">" & _
                                   "<Description>" +
dsGetBVItemDetails.Tables(0).Rows(i)(1).ToString() + "</Description>" & _
                               "</Item>" & _
                               "<ItemQty>" + dsGetBVItemDetails.Tables
(0).Rows(i)(2).ToString() + "</ItemQty>" & _
                               "<Message>" + dsGetBVItemDetails.Tables
(0).Rows(i)(3).ToString() + "</Message>" & _
                       i = i + 1

It is giving error while converting only when adding attribute “code” in element Item pls suggest

XFinalXML = XElement.Parse(strxmlString.ToString())

                   xmlResponse = XElement.Parse(XFinalXML.ToString())
                   xmlResponseDoc = New XDocument(xmlResponse)

pls help

Why don’t you grab the rows this way:


The reason it doesn’t work is because “row” has direct access the table, not “i”, or what you used.

Also, I would use a StringBuilder if you are using multiple strings. Saves resources.

It is due to the fact that the xml you are generating is not valid.

This line:
“<Item code=” + dsGetBVItemDetails.Tables
(0).Rows(i)(0).ToString() + “>”

should be:
“<Item code=\”" + dsGetBVItemDetails.Tables
(0).Rows(i)(0).ToString() + “\”>"

So? Wouldn’t my solution work as well? I have no idea why you would want to access the tables contents that way. Or, if it is possible. That’s why I said use the “row” in the “foreach”. If not, then what is the point of it?

Yes, your solution will work. I was merely pointing out that the output format of the xml was incorrect. Not what the data code should look like