Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
defined constant string
const string ConstStrCheck= "";

appending above
ConstStrCheck
in string builder object
like
StringBuilder strBody = new StringBuilder();
 strBody.AppendLine("<ISAPPLICABLE>" + ConstStrCheck+ " Applicable</ISAPPLICABLE><STATENAME>" + ConstStrCheck+ " Any</STATENAME>");

getting in string like this
<ISAPPLICABLE>&#4; Applicable</ISAPPLICABLE><STATENAME>&#4; Any</STATENAME>

but while writing to XML using
 XmlDocument doc = new XmlDocument();
doc.LoadXml(strBody.ToString())

getting XML like this
<isapplicable>&#x4; Applicable <statename>&#x4; Any

Getting
x
Extra in this string
&#4;


What I have tried:

not able to understand why its happening
Please Help me
Posted
Updated 4-Feb-18 6:00am
v2
Comments
Kornfeld Eliyahu Peter 29-Jan-18 17:16pm    
XML entities - those without specific names - has two equally valid formats...
&#nnn and &#xnnn - the first for decimal and the second for hexadecimal values...
As 4 is the same both in decimal and hexadecimal and XmlDocument always uses the hexadecimal format I would say all is good...
OPees 30-Jan-18 1:08am    
Understand the point thanks Kornfeld Eliyahu Peter
OPees 30-Jan-18 1:21am    
is there any way force to write in decimal only
Kornfeld Eliyahu Peter 30-Jan-18 3:13am    
You have to write your own XmlWriter and override the methods that handle entities to write them as decimal...
OPees 30-Jan-18 5:26am    
thanks

1 solution

You can use XDocument class[^].

For example:
const string ConstStrCheck= "&#4;";

XElement xroot = new XElement("MyRoot");
XElement xele1 = new XElement("ISAPPLICABLE", ConstStrCheck + " Applicable");
XElement xele2 = new XElement("STATENAME", ConstStrCheck + " Any");
xroot.Add(xele1);
xroot.Add(xele2);

XDocument xdoc = new XDocument();
xdoc.Add(xroot);


Result:
XML
<MyRoot>
  <ISAPPLICABLE>&#4; Applicable</ISAPPLICABLE>
  <STATENAME>&#4; Any</STATENAME>
</MyRoot>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900