jai krish
jai krish

Reputation: 691

How to remove xml "&" symbol

i am parasing xml file to dataset.I getting error if xml data contain "&" or some special char how to remove that?

How to remove "&" from below tag?

xml

 < department departmentid=1 name="pen & Note" >

$

string departmentpath = HostingEnvironment.MapPath("~/App_Data/Department.xml"); DataSet departmentDS = new DataSet(); System.IO.FileStream dpReadXml = new System.IO.FileStream(departmentpath, System.IO.FileMode.Open); try { departmentDS.ReadXml(dpReadXml); } catch (Exception ex) { //logg }

Upvotes: 1

Views: 1539

Answers (3)

Michael Kay
Michael Kay

Reputation: 163262

The culture of XML is that the person who creates the XML is responsible for delivering well-formed XML that conforms to the spec; the recipient is expected to reject it if they get it wrong. So by trying to repair bad XML and turn it into good XML you are going against the grain. It's like getting served bad food in a restaurant: you should complain, rather than asking the people at the next table how to make it digestible.

The input you've provided has a lot more wrong with it than the ampersands. It's hardly recognizable as XML at all. You're never going to turn this mess into a robust data flow.

Upvotes: 2

dirkgently
dirkgently

Reputation: 111120

The code seems to be C#. But do add the correct language tag!

There are five special characters that often require escaping within XML documents. You can read this SO question.

There are two possibilities:

  • Let your DataSet::ReadXML method handle these special characters [Recommended]
  • Change all special characters from your input files [Not recommended]

The second method is not recommended since you cannot possibly always control the incoming data (and you probably would be wasting time pre-processing them if you do want to). In order for ReadXML to properly parse the special characters you will need to define a proper encoding too in your input XML.

Upvotes: 1

gabitzish
gabitzish

Reputation: 9691

You can replace it with &amp;

Upvotes: 2

Related Questions