Link to home
Start Free TrialLog in
Avatar of sa3q
sa3q

asked on

when i try to read xml file in c# the unicode appear like ?????????? questions mark how can i solve it

hello,

when i try  to  use the code  below to  change  & to -->  &

i found that the Unicode  that included  in  the XML  file  appear  like question  mark  ?????????


how can i solve it ???

 thank  you  
XmlDocument doc = new XmlDocument ();
string s = System.Text.RegularExpressions.Regex.Replace (System.IO.File.ReadAllText (@"D:\x.xml").ToString (), "&", "&");
doc.LoadXml (s);

Open in new window

Avatar of pradyahuja
pradyahuja
Flag of Australia image


XmlDocument doc = new XmlDocument ();

string s =Server.HtmlEncode(System.IO.File.ReadAllText (@"D:\x.xml").ToString ());
doc.LoadXml (s);

Open in new window

Hi,

I tried the above it works, I mean it replaces perfectly, Can you post you xml file?

Thanks,
Arun
Avatar of sa3q
sa3q

ASKER

i know that it make replace  but  after i do  replace i want to  read  the  tags  and if the attributes have any  non  English  word  it appear  to  me like that  (?????)
thats why I suggested HTMLEncode
Avatar of sa3q

ASKER

pradyahuja:>>

my  code run  on  application  not on  web  
Avatar of sa3q

ASKER

Server

 not defined
try
HttpUtility.HtmlEncode
Avatar of sa3q

ASKER

what  name space  that have  this class
System.Web
Hi,

Post you xml file.

Thanks,
Arun.
Avatar of sa3q

ASKER

this  what happened
Capture.PNG
Hi,

If its possible provide your original xml file. [If you have any importantant info, modify the content and post it]

Thanks,
Arun
Avatar of sa3q

ASKER

this is sample xml  file

the data  in  arabic  language  not  appear  but appear  like ???????

when  i use  the dom  to  load the xml


----------------------
                XmlDocument dom = new XmlDocument();

                string s = System.Text.RegularExpressions.Regex.Replace(System.IO.File.ReadAllText(textBox1.Text).ToString(), "&", "&");
           
                dom.LoadXml(s);
<?xml version="1.0" encoding="Windows-1256"?>
<directory name="c:">
	<directory name="c:\k">
		
			<file name="¿¿¿¿¿¿.txt" size="544"/>
			<file name="¿¿¿¿¿¿.txt" size="544"/>
			<file name="¿¿¿¿¿¿.txt" size="544"/>
			<file name="$&ITIDIXW" size="544"/>
			<file name="$IZ7AR8A&df.exe" size="544"/>
			
		</directory>
	</directory>

Open in new window

Avatar of sa3q

ASKER

this is the original file  for  the  xml  file because  their  problems when  i attached the code
orignial.xml
did u try
System.Web.HttpUtility.HtmlEncode
Avatar of sa3q

ASKER

when  i use
dom.LoadXml(xml_String );
the arabic language  appear ???????

but when  i use

//dom.Load(@"c:\\file.xml");

the arabic language appear  ok  
Avatar of sa3q

ASKER

i tried  System.Web.HttpUtility.HtmlEncode

but  HttpUtility.HtmlEncode

not found in this name space
did u try this code?
XmlDocument doc = new XmlDocument ();

string s =System.Web.HttpUtility.HtmlEncode(System.IO.File.ReadAllText (@"D:\x.xml").ToString ());
doc.LoadXml (s);

Open in new window

Avatar of sa3q

ASKER

i used  this code  before  and the  name space  for  the HttpUtility  not  found
Avatar of sa3q

ASKER

to  find  where is the problem  i used this function  to  make replace  of "&" to "&amp;"  in original.xml  file

and i found that   this function (System.Text.RegularExpressions.Regex.Replace( content, searchText, replaceText );)  destroy  any  Unicode letters in  the file  

how can  i make replace  with out  damage unicode characters

static public void ReplaceInFile( string filePath, string searchText, string replaceText )
{
    StreamReader reader = new StreamReader( filePath );
    string content = reader.ReadToEnd();
    reader.Close();

    content = System.Text.RegularExpressions.Regex.Replace( content, searchText, replaceText );

    StreamWriter writer = new StreamWriter( filePath );
    writer.Write( content );
    writer.Close();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sa3q

ASKER

thanks very  much   best  solution  i have seen