views:

32

answers:

3
+1  Q: 

XML Parsing failed

I have this in a XML doc.

<userdata name='filter'>&SearchExpression[0].Key=Id&SearchExpression[0].Value=1&</userdata>

The value is a query which will be added to an url.

It keeps failing to parse. It says the document isn't well formatted. The parser is pointing at the bracket "["

Could the bracket causing the error ?

How can I parse a XML file with a bracket ?

+3  A: 

I suspect it's actually due to the ampersands. Ampersands need to be escaped in XML, as &amp;. Your XML should be:

<userdata name='filter'>&amp;SearchExpression[0].Key=Id&amp;[...]</userdata>

My guess is that it gave an error at [ because it was expecting a semi-colon, thinking that SearchExpression was an entity name (which can't include [).

Was this document hand-edited, or created by a program? If it was autogenerated (and it was your code) then you should start using XML APIs to create the XML, rather than manually writing out strings - they handle all of this for you.

Jon Skeet
+4  A: 

Change your xml to this:

<userdata name='filter'><![CDATA[&SearchExpression[0].Key=Id&SearchExpression[0].Value=1&]]></userdata>

It means the same, but allows you to have special characters in the element inner text.
You can read more about CDATA here.

ŁukaszW.pl
+1  A: 

The problem comes from the unfinished escape sequences starting with & you need to either wrap the element content in <![CDATA[ ]]>, or escape the & thus: &amp;

Paul Butcher