Raghubar
Raghubar

Reputation: 2788

Not able to convert a string to XML

I am passing XML as a string to a method and again converting that in XML to do my work.

Its working fine normally, but when there are special character like & or = it's giving an error.

My XML string:

<SuggestedReadings>
   <Suggestion Text="Customer Centricity" Link="http://wdp.wharton.upenn.edu/book/customer-centricity/?utm_source=Coursera&utm_medium=Web&utm_campaign=custcent" SuggBy="Pete Fader�s" />
   <Suggestion Text="Global Brand Power" Link="http://wdp.wharton.upenn.edu/books/global-brand-power/?utm_source=Coursera&utm_medium=Web&utm_campaign=glbrpower" SuggBy="Barbara Kahn�s" />
</SuggestedReadings>

My code is:

public class saveData(string strXml)
{
      XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.LoadXml(CD.SRList);// here its giving error
}

Error :

'=' is an unexpected token. The expected token is ';'. Line 1, position 150.

Complete error is:

System.Xml.XmlException was unhandled by user code HResult=-2146232000 Message='=' is an unexpected token. The expected token is ';'. Line 1, position 150. Source=System.Xml LineNumber=1 LinePosition=150 SourceUri="" StackTrace: at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args) at System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(String expectedToken1, String expectedToken2) at System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(Int32 pos, String expectedToken1, String expectedToken2) at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32& charRefEndPos) at System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr) at System.Xml.XmlTextReaderImpl.ParseAttributes() at System.Xml.XmlTextReaderImpl.ParseElement() at System.Xml.XmlTextReaderImpl.ParseElementContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at System.Xml.XmlDocument.LoadXml(String xml) at ICA.LMS.Service.Controllers.AdminCourseApiController.SaveCourse(CourseDetails CD) in d:\Live Projects\ICA_LMS\ICA_LMS_WebAPI\Controllers\AdminCourseApiController.cs:line 122 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) InnerException:

Upvotes: 3

Views: 3370

Answers (4)

MKEF
MKEF

Reputation: 173

XML predefines the following five entity references for special characters that would otherwise be interpreted as part of markup language

<   ->  &lt;
>   ->  &gt;
"   ->  &quot;
'   ->  &apos;
&   ->  &amp;

You can use entity and character references to escape the left angle bracket, the ampersand, and other delimiters. You can also use numeric character references. Numeric character references are expanded immediately when they are recognized. In addition, because numeric character references are treated as character data, you can use the numeric character references

Hope this links will help you.

How to locate and replace special characters in an XML file with Visual C# .NET https://support.microsoft.com/en-us/kb/316063

Different ways how to escape an XML string in C# http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 156938

Your document is missing the XML header, which is required. Also, you are not correctly escaping the & character.

Try adding this on top of your XML document:

<?xml version="1.0" encoding="UTF-8"?>

And also replace & with &amp;. (See the list of characters to escape)

Upvotes: 3

Ewan
Ewan

Reputation: 1285

its actually the ampersand which is incorrectly escaped. its just expecting it to be an escaped character in the form &something; so when it gets to the = it throws the error.

[TestMethod]
public void BadXml()
{
    string xml = "<SuggestedReadings><Suggestion Text=\"Customer Centricity\" Link=\"http://wdp.wharton.upenn.edu/book/customer-centricity/?utm_source=Coursera&utm_medium=Web&utm_campaign=custcent\" SuggBy=\"Pete Fader�s\" /><Suggestion Text=\"Global Brand Power\" Link=\"http://wdp.wharton.upenn.edu/books/global-brand-power/?utm_source=Coursera&utm_medium=Web&utm_campaign=glbrpower\" SuggBy=\"Barbara Kahn�s\" /></SuggestedReadings>";

    XmlDocument xdoc = new XmlDocument();
    xml = xml.Replace("&", "&amp;");
    xdoc.LoadXml(xml);

}

Upvotes: 1

Pajdziu
Pajdziu

Reputation: 920

& is special character in XML. Try to use &amp; instead of &

<SuggestedReadings>
   <Suggestion Text="Customer Centricity" Link="http://wdp.wharton.upenn.edu/book/customer-centricity/?utm_source=Coursera&amp;utm_medium=Web&amp;utm_campaign=custcent" SuggBy="Pete Fader�s" />
   <Suggestion Text="Global Brand Power" Link="http://wdp.wharton.upenn.edu/books/global-brand-power/?utm_source=Coursera&amp;utm_medium=Web&amp;utm_campaign=glbrpower" SuggBy="Barbara Kahn�s" />
</SuggestedReadings>

Upvotes: 2

Related Questions