Unable to convert string to XML

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

Works fine fine, but when there is a special character like &

or =

, it gives 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:

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

      

Mistake:

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

Full error:

System.Xml.XmlException was not handled 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.XmlTextReaderEdanimpl.Health 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.Xml.Read () at System.Xml.Xml.Read () at System.Xml. System.Xml.XmlLoader.LoadDocSequence (XmlDocument parentDoc) in System.Xml.XmlLoader.Load (XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) in System.Xml.XmlDocument.Load (XmlReaderml.Xml reader) in System.Xcument String xml) in ICA.LMS.Service.Controllers.AdminCourseApiController.SaveCourse (CourseDetails CD) in d: \ Live Projects \ ICA_LMS \ ICA_LMS_WebAPI \ Controllers \ AdminCourseApiController.cs: line 122 in lambda_method (Closure), Object, Object [ System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor <. > C__DisplayClass10.b__9 (Object instance,Object [] methodParameters) in System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute (Instance object, Object []) in System.Web.Http.Controllers.ReflectedHttpActionDescriptor. canceecuteokenAsync (HttpActionDescriptor. canceecuteokenAsync (HttpActionControllerConference) CancellationTokenTokenTopics, CancellationTokenText) ) InnerException:

+3


source to share


4 answers


Your document is missing the XML header you require. Also, you have not selected a symbol &

.

Try adding this on top of your XML document:



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

      

And also replace &

with &amp;

. (See list of symbols for exit )

+3


source


&

is a special character in XML. Try using &amp;

instead&



<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>

      

+2


source


its actually an ampersand, which is incorrectly escaped. he was simply expected to become a fugitive character in the form of & something; so when it gets value = it throws an 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);

}

      

+1


source


XML predefines the following five entity references for special characters that are otherwise interpreted as part of the markup Language

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

      

You can use object and symbol references to avoid left angle brackets, ampersands, and other delimiters. You can also use numeric character references. Numeric character references are expanded as soon as they are recognized. Also, since numeric symbolic links are treated as symbolic data, you can use numeric symbolic links

Hope these links help you.

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

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

+1


source







All Articles