parsing this XML in C# for a SharePoint Field
-
07-10-2020 - |
Pregunta
I have the below field xml which I get after calling a web service. I am getting the error
Name cannot begin with the '=' character, hexadecimal value 0x3D. Line 1, position 2325.
<Field DisplayName='Maximum_x0020_characters_x0020_a' FillInChoice='TRUE' Format ='RadioButtons' Type ='Choice' StaticName ='Maximum_x0020_characters_x0020_a' Required='TRUE' Name='Maximum_x0020_characters_x0020_a'><Default></Default><CHOICES><CHOICE>True</CHOICE><CHOICE>False</CHOICE><CHOICE>True</CHOICE><CHOICE>False</CHOICE><CHOICE>>=255</CHOICE><CHOICE><=255</CHOICE><CHOICE>>=510</CHOICE><CHOICE><= 510</CHOICE></CHOICES></Field>
Can anyone please suggest the correct way of replacing the special character. I tried replacing <
and >
for >
and <
but then I get another error.
Solución
I added the special character part in CDATA tag and it worked. For Example I was having the XML of the choice field where choices were coming with special characters which was causing the issue. To overcome it, I added the choice tag of XML in the *** part and it worked.
CDATA takes the value as simple text.
str.Append("<CHOICE><![CDATA[" + choices[k] + "]]></CHOICE>");
Choices[k] are my choice value which I am fetching from code.
Otros consejos
If you create a choice column using the browser and enter >=250
<=510
etc as choices, and then look at the SchemaXml
property of the column (for example using a tool like SharePoint Manager), you can see how they are encoded internally by SharePoint, and help troubleshooting. In this case:
<CHOICE>>=255</CHOICE>
<CHOICE><=255</CHOICE>
<CHOICE>>=510</CHOICE>
Perhaps you were missing the semicolon (;
) from lt;
and gt;
.
CDATA is also a valid option, but for simple cases like these when only a character needs to be encoded, encoding them individually makes the code cleaner.