陷入 for c# 循环!

发布于 2024-10-24 06:39:22 字数 3655 浏览 1 评论 0原文

我有两个 xml 文件,其中有 devanagri 到 itrans 映射,这些文件是 dev.xml 和古吉拉特语.xml。 xml 文件内容如下:

对于 guj.xml 它是:

<mapping>
  <character>અ</character>
  <itrans>a</itrans>
</mapping>

对于 dev.xml 它是

<mapping>
    <character>अ</character>
    <itrans>a</itrans>
</mapping>

我有一个 global.asax 文件,其中包含以下代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using System.Xml;

namespace finAL
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            sbyte[,] d = new sbyte[100, 100];
            sbyte[,] g = new sbyte[100, 100];
            int count = 0;
            int j = 0;

            XmlDocument docA = new XmlDocument();
            StreamReader sr = new StreamReader("dev.xml");
            docA.Load(sr);

            XmlDocument docB = new XmlDocument();
            StreamReader sr1 = new StreamReader("guj.xml");
            docB.Load(sr1);

            XmlNodeList elemlist1 = root.GetElementsByTagName("mapping");
            XmlNodeList elemlist2 = root.GetElementsByTagName("mapping");
            XmlNodeList X;

            for (int i = 0; i < elemlist1.Count; i++)
            {
                X = elemlist1[i].GetElementsByTagName("itrans");
                d[i][0] = X[0].firstchild.data;


                X = elemlist1[i].GetElementsByTagName("character");
                d[i][1] = X[0].firstchild.data;
            }

            for (int i = 0; i < elemlist2.Count; i++)
            {
                X = elemlist1[i].GetElementsByTagName("itrans");
                g[i][0] = X[0].firstchild.data;

                X = elemlist1[i].GetElementsByTagName("character");
                g[i][1] = X[0].firstchild.data;
            }

            Session("dtable") = d;
            Session("gtable") = g;
        }
    }
}

这里的代码从两个 xml 文件中读取并将字符存储在二维数组。该数组进一步存储在会话变量中,以便我可以在default.aspx 中访问它。default.aspx

文件包含以下代码。

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace finAL
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            sbyte[,] TableD = new sbyte[100, 100];
            sbyte[,] TableG = new sbyte[100, 100];
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<div>" & Request.Form("inTransContent")   & "</div>");
            XmlNodeList nodeList;
            nodeList = doc.DocumentElement.SelectNodes("//text()");
            TableD = Session("dtable");
            TableG = Session("gtable");
            String str=tmpNode.data;
            str.ToCharArray();
            foreach (XmlText tmpNode in nodeList)
            {

            }
            Response.Write(doc.DocumentElement.InnerXml);
        }
    }
}

这里的 intranscontent 包含带有 div 元素的网页。 所以我从中选择文本节点。在此代码中,我正在访问 global.asax 中的会话变量,节点将转换为字符串,然后转换为 chararray,以便我可以将 devanagri 字符替换为古吉拉特语。

在 for 循环中,我需要检查每个 devanagri 字符是否有一个等效的古吉拉特语字符,同时牢记所有音译规则。音译规则就像如果前一个字符是元音,那么它应该只输出元音。如果有人能帮助我完成这个 for 循环,我将非常感谢他们。谢谢!如有任何疑问,请随时发布。

i have two xml files which have devanagri to itrans mapping, these files are dev.xml
and gujarati.xml. The xml file contents are as follows:

for guj.xml it is:

<mapping>
  <character>અ</character>
  <itrans>a</itrans>
</mapping>

and for dev.xml it is

<mapping>
    <character>अ</character>
    <itrans>a</itrans>
</mapping>

I have a global.asax file which has the following code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using System.Xml;

namespace finAL
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            sbyte[,] d = new sbyte[100, 100];
            sbyte[,] g = new sbyte[100, 100];
            int count = 0;
            int j = 0;

            XmlDocument docA = new XmlDocument();
            StreamReader sr = new StreamReader("dev.xml");
            docA.Load(sr);

            XmlDocument docB = new XmlDocument();
            StreamReader sr1 = new StreamReader("guj.xml");
            docB.Load(sr1);

            XmlNodeList elemlist1 = root.GetElementsByTagName("mapping");
            XmlNodeList elemlist2 = root.GetElementsByTagName("mapping");
            XmlNodeList X;

            for (int i = 0; i < elemlist1.Count; i++)
            {
                X = elemlist1[i].GetElementsByTagName("itrans");
                d[i][0] = X[0].firstchild.data;


                X = elemlist1[i].GetElementsByTagName("character");
                d[i][1] = X[0].firstchild.data;
            }

            for (int i = 0; i < elemlist2.Count; i++)
            {
                X = elemlist1[i].GetElementsByTagName("itrans");
                g[i][0] = X[0].firstchild.data;

                X = elemlist1[i].GetElementsByTagName("character");
                g[i][1] = X[0].firstchild.data;
            }

            Session("dtable") = d;
            Session("gtable") = g;
        }
    }
}

The code here reads from the both the xml files and stores the characters in a two dimensional array. This array isfurther stored in a session variable so that I can access it in default.aspx

The default.aspx file contains the following code.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace finAL
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            sbyte[,] TableD = new sbyte[100, 100];
            sbyte[,] TableG = new sbyte[100, 100];
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<div>" & Request.Form("inTransContent")   & "</div>");
            XmlNodeList nodeList;
            nodeList = doc.DocumentElement.SelectNodes("//text()");
            TableD = Session("dtable");
            TableG = Session("gtable");
            String str=tmpNode.data;
            str.ToCharArray();
            foreach (XmlText tmpNode in nodeList)
            {

            }
            Response.Write(doc.DocumentElement.InnerXml);
        }
    }
}

here the intranscontent contains the webpage with the div element.
so I select the textnodes from them. In this code I am accessing the session variable which were in the global.asax the nodes are converted to strings and then to chararray so that I can replace the characters of devanagri to gujarati.

In the for loop I need to check for every devanagri character an equivalent gujarati character keeping in mind all the transliteration rules.the transliteration rules are like if the previous character is vowel then it should output vowel only. If anyone could help me with this for loop I will be thankful to them. Thanks! any queries plz free to post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2024-10-31 06:39:22

如果 itrans 确实是 Devanagri 和古吉拉特语字符之间的唯一标识符(我对此表示怀疑),那么我建议创建一个 Dictionary而不是数组。

var gujarati = "<mappings><mapping><character>અ</character><itrans>a</itrans></mapping><mapping><character>ત</character><itrans>i</itrans></mapping></mappings>";
var devanagri = "<mappings><mapping><character>अ</character><itrans>a</itrans></mapping><mapping><character>र</character><itrans>i</itrans></mapping></mappings>";

var gujDict = XDocument.Parse(gujarati).
                Descendants("mapping").
                ToDictionary(ele => ele.Element("itrans").Value[0], ele => ele.Element("character").Value[0]);
var devDict = XDocument.Parse(devanagri).
                Descendants("mapping").
                ToDictionary(ele => ele.Element("itrans").Value[0], ele => ele.Element("character").Value[0]);

var devToGuj = new Dictionary<char, char>();

foreach (var c in devDict) 
{
    char dev;
    if (gujDict.TryGetValue(c.Key, out dev))
        devToGuj.Add(c.Value, dev);
}

然后你只需 devToGuj['અ'] 即可获取相应的字符。要替换字符串中的字符,您可以执行类似的操作。

var devText = "अ quरck test.";
var gujText = String.Join("", 
                    devText.ToArray().
                    Select(c => devToGuj.ContainsKey(c) ? devToGuj[c] : c)
                    );

=> “અ quતck 测试。”

If the itrans really are a unique identifier, which I doubt, between Devanagri and Gujarati characters then I would recommend creating a Dictionary<char,char> instead of an array.

var gujarati = "<mappings><mapping><character>અ</character><itrans>a</itrans></mapping><mapping><character>ત</character><itrans>i</itrans></mapping></mappings>";
var devanagri = "<mappings><mapping><character>अ</character><itrans>a</itrans></mapping><mapping><character>र</character><itrans>i</itrans></mapping></mappings>";

var gujDict = XDocument.Parse(gujarati).
                Descendants("mapping").
                ToDictionary(ele => ele.Element("itrans").Value[0], ele => ele.Element("character").Value[0]);
var devDict = XDocument.Parse(devanagri).
                Descendants("mapping").
                ToDictionary(ele => ele.Element("itrans").Value[0], ele => ele.Element("character").Value[0]);

var devToGuj = new Dictionary<char, char>();

foreach (var c in devDict) 
{
    char dev;
    if (gujDict.TryGetValue(c.Key, out dev))
        devToGuj.Add(c.Value, dev);
}

Then you just devToGuj['અ'] to get the corresponding character. To replace the characters in a string you could do something like this.

var devText = "अ quरck test.";
var gujText = String.Join("", 
                    devText.ToArray().
                    Select(c => devToGuj.ContainsKey(c) ? devToGuj[c] : c)
                    );

=> "અ quતck test."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文