0

xml file-> for example this is hosted on the url http://localhost/test1

<?xml version="1.0" encoding="utf-8"?> 
<MSG>
    <arabic>
        <translationOne>اول</translationOne>
        <translationTwo>دوم</translationTwo>
    </arabic> 
     <persian>
          <translationOne>یک</translationOne>
          <translationTwo>دوم</translationTwo>
    </persian> 
</MSG>

c# class

            var m_strFilePath = "http://localhost/test1";
            string xmlStr;
            using (var wc = new WebClient())
            {
                xmlStr = wc.DownloadString(m_strFilePath);
            }
            var xmldoc = new XmlDocument();
            xmldoc.LoadXml(xmlStr);
            XmlNodeList unNodeA = xmldoc.SelectNodes("MSG/arabic");
            XmlNodeList unNodeP = xmldoc.SelectNodes("MSG/persian");
            string arabic = "";
            foreach (XmlNode i in unNodeA)
            {
                 arabic += i["translationOne"].InnerText;
            }
            string persian= "";
            string persian2 ="";
            foreach (XmlNode ii in unNodeP)
            {
                 persian+= ii["translationOne"].InnerText;
                 persian2+= ii["translationTwo"].InnerText;
            }
             
            ->>print(arabic and persian);

here the test contain not correct format like (اول دوم) it's some kind of (عبد العزيز عباسین)

SAR
  • 1,765
  • 3
  • 18
  • 42
  • Is your console UTF-8? Did you check if the strings actually are correct and the output is just wrong? (Also System.out.print is Java, not C#, so not sure how this code would work) – Sami Kuhmonen Feb 17 '21 at 15:42
  • yes i am just printing this sorry i mixed these too it's getting print but the encoding is not correct, i search they told to parse but i am fetching this from url – SAR Feb 17 '21 at 15:44
  • See https://stackoverflow.com/questions/310669/why-does-c-sharp-xmldocument-loadxmlstring-fail-when-an-xml-header-is-included – Tu deschizi eu inchid Feb 17 '21 at 16:04

1 Answers1

1

It is better to use LINQ to XML API. It is available in the .Net Framework since 2007.

c#

void Main()
{
    XDocument xdoc = XDocument.Parse(@"<?xml version='1.0' encoding='utf-8'?><MSG>
            <arabic>
                <translationOne>اول</translationOne>
                <translationTwo>دوم</translationTwo>
            </arabic> 
             <persian>
                  <translationOne>یک</translationOne>
                  <translationTwo>دوم</translationTwo>
            </persian> 
        </MSG>");

    foreach (XElement elem in xdoc.Descendants("arabic").Elements())
    {
        Console.WriteLine("translation: {0}", elem.Value);
    }
}

If you need to load XML from the URL:

const string Url = @"http://hurt.super-toys.pl/xml/super_toys_ceneo_pelny.xml";
XDocument xdoc = XDocument.Load(Url);

Output

translation: اول
translation: دوم
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21