Click here to Skip to main content
14,384,533 members
Rate this:
Please Sign up or sign in to vote.
See more:
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);
           }
Posted
Updated 2-Dec-18 0:51am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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 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>
   
Comments
Member 13050667 1-Dec-18 16:55pm
   
Thanks for taking the time to help me!
Unfortunately, your method returns those nodes in a separate loop. However, I have to save 4 node content from a <tu>. (what I asked was just the problematic part 2 nodes from the 4) is there a better way? I mean other methods next to LINQ... which can work?!
Rate this:
Please Sign up or sign in to vote.

Solution 2

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 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>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100