C#:如何使用LINQ从XML读取类似的节点 [英] C#: how to read similar nodes from XML with LINQ

查看:81
本文介绍了C#:如何使用LINQ从XML读取类似的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个更大的XML文件,我想读到我的应用程序并将一些值存储在自定义类中。在该文件中,有几个< tu>节点。 (新的TUs类/ tu部分)。有问题的部分是< seg>节点。 (每个< tu>具有两个具有相同父节点但具有不同属性(语言)的< seg>节点)。

如您所见,我的基本脚本将捕获相同的(第一个)< ; SEG>节点两次。



I have a bigger XML file what I wish to read to my app and store some of the values in a custom class. In the file, there are several <tu> nodes. (new TUs class / tu section). The Problematic part is with <seg> node. (each <tu> has two <seg> nodes with the same parent node but with different attribute (language)).
As you can see my basic script will catch the same (first) <seg> node twice.

<tu creationdate="20130619T135814Z" creationid="ANA" changedate="20130619T135814Z" changeid="ANA" lastusagedate="20130619T135814Z">
      <prop type="x-LastUsedBy">ANA</prop>
      <prop type="x-Origin">TM</prop>
      <prop type="x-OriginalFormat">TradosTranslatorsWorkbench</prop>
      <tuv xml:lang="de-DE">
        <seg>Die Teile werden im Querdurchlauf transportiert.<ph x="1" type="1" /></seg>
      </tuv>
      <tuv xml:lang="en-GB">
        <seg>The parts are transported in a cross conveyance.<ph x="1" type="1" /></seg>
      </tuv>
    </tu>





我尝试过:





What I have tried:

XDocument xDoc;
           xDoc = XDocument.Load(path);

           var result = from q in xDoc.Descendants("tu")
                        select new TUs
                        {
                            SOURCE = q.Element("tuv").Element("seg").Value,
                            TARGET = q.Element("tuv").Element("seg").Value,
                        };

           foreach (TUs TU in result)
           {
               Console.WriteLine(TU.SOURCE);
               Console.WriteLine(TU.TARGET);
           }

推荐答案

这只有在您的XML文件格式正确时才有效:

This can only work if your XML file is correctly formatted:
XDocument xDoc;
xDoc = XDocument.Load(path);

foreach (var node in xDoc.Root.Descendants().Where(n => n.Name == "seg"))
{
    Console.WriteLine(node.Name + " = " + node.Value);
}



XML数据:


XML data:

<tu creationdate="20130619T135814Z" creationid="John" changedate="20130619T135814Z" changeid="John" lastusagedate="20130619T135814Z">
  <prop type="x-LastUsedBy">
    John
    <prop type="x-Origin">
      TM
      <prop type="x-OriginalFormat">
        TestTool
        <tuv xml:lang="de-DE">
          <seg>This is the German text</seg>
        </tuv>
        <tuv xml:lang="en-GB">
          <seg>This is the English text</seg>
        </tuv>
      </prop>
    </prop>
  </prop>
</tu>


这是一个适用于多个tu节点的解决方案:

Here is a solution that works better with multiple tu nodes:
XDocument xDoc = XDocument.Load(path);

foreach (XElement tu in xDoc.Root.Descendants("tu"))
{
    Console.WriteLine("tu = " + tu.Attribute("creationid").Value);

    foreach (XElement tuv in tu.Descendants("tuv"))
    {
        if (tuv.ToString().Contains("\"en-GB\""))
        {
            Console.WriteLine("seg = " + tuv.Descendants("seg").First().Value);
        }
    }

    Console.WriteLine();
}



XML数据必须具有Root元素:


XML data must have a Root element:

<Root>
<tu creationdate="20130619T135814Z" creationid="John" changedate="20130619T135814Z" changeid="John" lastusagedate="20130619T135814Z">
  <prop type="x-LastUsedBy">John</prop>
  <prop type="x-Origin">TM</prop>
  <prop type="x-OriginalFormat">TestTool</prop>
  <tuv xml:lang="de-DE">
    <seg>This is the German text</seg>
  </tuv>
  <tuv xml:lang="en-GB">
    <seg>This is the English text</seg>
  </tuv>
</tu>
<tu creationdate="20130619T135814Z" creationid="Rick" changedate="20130619T135814Z" changeid="Rick" lastusagedate="20130619T135814Z">
  <prop type="x-LastUsedBy">Rick</prop>
  <prop type="x-Origin">TM</prop>
  <prop type="x-OriginalFormat">TestTool</prop>
  <tuv xml:lang="de-DE">
    <seg>This is Ricks German text</seg>
  </tuv>
  <tuv xml:lang="en-GB">
    <seg>This is Ricks English text</seg>
  </tuv>
</tu>
</Root>


这篇关于C#:如何使用LINQ从XML读取类似的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆