SQL Server XML转换报错:XML解析第3行第119位缺少等号,求解决
Hey there, let's break down why your XML conversion is throwing that error and how to fix it quickly.
The Root Cause
SQL's convert(XML, ...) function enforces strict well-formed XML rules, and your HTML string has invalid syntax for XML: unquoted, value-less attributes. In HTML, you can write attributes like DISABLED or REQUIRED without assigning a value, but XML requires every attribute to have a name-value pair (e.g., DISABLED="DISABLED" or DISABLED="").
Looking at your input string, the problematic parts are:
<input ... DISABLED>(line 3, around character 119)<textarea ... DISABLED REQUIRED>
XML parsers see these and throw the "equal expected" error because they're expecting an = sign followed by a quoted value after the attribute name.
The Fix
Update your input string to convert those value-less attributes into XML-compliant form. The most common approach is to set the attribute value to match its name (e.g., DISABLED="DISABLED"), though you can also use an empty string if preferred.
Here's the corrected SQL code:
Create table #temp ( NAME VARCHAR(MAX) ) Insert Into #temp select '<div class="form-group form-row"><label class="col-sm-5 control-label">Account Name</label><div class="form-controls"><div class="form-control-static">Bond</div></div></div><div class="form-group form-row"><label class="col-sm-5 control-label">Account Number</label><div class="form-controls"><div class="form-control-static">PQR</div></div></div><div class="form-group"> <label class="control-label">Specific subject</label> <input class="input-text form-control" type="text" name="Specific subject" size="20" value="Money Transfer" DISABLED="DISABLED"> </div><div class="form-group"> <label class="control-label mandatory" for="message">Message</label> <textarea class="textarea form-control" name="message" id="message" cols="30" rows="3" maxlength="5000" DISABLED="DISABLED" REQUIRED="REQUIRED"> The Name is XYZ </textarea><span class="mand">*</span><span class="help-text">Max. 5000 characters</span> </div>' select convert(XML, NAME) from #temp
Key Changes Made
- Changed
DISABLEDtoDISABLED="DISABLED"in both the<input>and<textarea>elements - Changed
REQUIREDtoREQUIRED="REQUIRED"in the<textarea>element
Run this corrected code, and your XML conversion should work without parsing errors.
内容的提问来源于stack exchange,提问作者som_1522

