C# - 如何拆分,替换xml文件的内容?

时间:2014-01-08 10:38:09

标签: c# regex

我有一个xml文件:

<xml ?iwes sf>
    <product>
        <name> Computer </name>
        <details>a product of Dell</details>
        <sender> sender no name</sender>
    </product>
</xml>

我希望分成:

<xml ?iwes sf>,     ,<product>,         ,<name>,Computer,</name>,...,    ,</xml>

然后我将文本内容(计算机,戴尔的产品,发件人没有名字)翻译成韩语。 最后,我想加入

<xml ?iwes sf>,     ,<product>,         ,<name>,Computer,</name>,...,    ,</xml>

结果已翻译:

<xml ?iwes sf>
    <product>
        <name> 컴퓨터</name>
        <details>Dell의 제품 </details>
        <sender> 아니오 유명한 송신기 </sender>
    </product>
</xml>

我用了Regex:

string[] gettag = Regex.Split(inputText.Text, "(<.*?>)|(.+?(?=<|$))");

但它错了,它不能做我想要的,我不能将xml标签加入到位置之前,因为它不能得到“\ t”! 我该怎么办?? 我的英语水平不高。我希望每个人都能理解我所说的并帮助我! 谢谢!

1 个答案:

答案 0 :(得分:0)

y不是一些lambda表达式吗?

你可以试试这个。

      IEnumerable<XElement> xx = xdoc.Root.Descendants().Elements().Where(c => { if (c.Name == "name") { c.Value = "Dell"; } else if (c.Name == "details") { c.Value = "Dell Detial"; } return true; }).Select(c => c);


            // Loop.
            string x = string.Empty;
            foreach (XElement x1 in xx)
            {
                x += x1.ToString();
            }

      Console.WriteLine(x.ToString());