0

I'm trying to find out how to get translated memory from tmx files (i.e. XML files).

Here is an extract of a tmx file I'm working on.

<tmx>
 <body>
  <tu tuid="1">
   <tuv xml:lang="en-US">
    <seg>Memory</seg>
   </tuv>
   <tuv xml:lang="ar">
    <seg>XXXXX</seg>
   </tuv>
  </tu>
  <tu tuid="2">
   <tuv xml:lang="en-US">
    <seg>Address</seg>
   </tuv>
   <tuv xml:lang="ar">
    <seg>yyyyy</seg>
   </tuv>
  </tu>
//
<body>
<tmx>

What I'm attempting to do is to search for a particular value inside "seg", and then get the corresponding value inside the next "seg".

In the above example, if I'm looking for

<seg>Memory<seg>

then, I need to retrieve

<seg>xxxxx<seg>

My coding is like this.

DataRow[] searchWordsArray = keyStrings.ToArray();
XmlNodeList nodelist = doc.SelectNodes("//body//tu");

for (int i = 0; i < searchWordsArray.Length; i++){
    for (int n = 0; n < nodelist.Count; n++) {

         string searchWord = searchWordsArray[i][KeyCol].ToString();
         string result = "";
         if (searchWord == nodelist[n].SelectSingleNode("/tuv[1]/seg").InnerText) {
             result = nodelist[n].SelectSingleNode("/tuv[2]/seg").InnerText;
         } else {
             result = "No match";
         }
    }
}

However, this code gets null reference exception at

if (searchWord == nodelist[n].SelectSingleNode("/tuv[1]/seg").InnerText) {

I guess my Xpath expression contains some error. I'd appreciate if you would give any insight.

Hiroki
  • 3,893
  • 13
  • 51
  • 90
  • Your XPath expressions are perfectly fine. They just don't select anything in XML you've provided. Check out any XPath reference or quick start (i.e. [Xpath Examples on MSDN](http://msdn.microsoft.com/en-us/library/ms256086%28v=vs.110%29.aspx) ) to confirm than tyou correctly understand difference between `/` and `//` in XPath. – Alexei Levenkov Oct 03 '14 at 04:53
  • You're already in the `tu` node so don't do `/tuv[x]/...`. Use `tuv[x]/...` instead. As Alexei mentions, read up on what the `/` and `//` do. – TyCobb Oct 03 '14 at 05:16
  • your xml isn't valid, the tmx and body tags don't have closing tags. If you mean the bottom two tags to be them... – Chuck Savage Oct 03 '14 at 09:28

2 Answers2

0

You need to add leading dot (.) or remove the leading slash (/) for the XPath to be recognized as relative path :

if (searchWord == nodelist[n].SelectSingleNode("./tuv[1]/seg").InnerText) {
     result = nodelist[n].SelectSingleNode("./tuv[2]/seg").InnerText;
}

or :

if (searchWord == nodelist[n].SelectSingleNode("tuv[1]/seg").InnerText) {
     result = nodelist[n].SelectSingleNode("tuv[2]/seg").InnerText;
}
har07
  • 88,338
  • 12
  • 84
  • 137
0

Do you have to use XPath?

Using Linq-To-Xml

XElement root = XElement.Load(file);
var segs = root.Descendants("seg");
XElement check = null;
var resultNode = segs.FirstOrDefault(x => 
{
    if ((string)check == "Memory")
        return true;
    check = x;
    return false;
});
string result = (string)resultNode;
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69