Jason
Jason

Reputation: 371

XElement parsing from XDocument, far below, repeated

I would like to make my result like this;

//  Boo1
//  Boo2
//  Boo3
//  ....
//  ..
//  .

Frome here..

//<xliff xmlns:sdl="http://sdl.com/FileTypes/SdlXliff/1.0" xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2" sdl: version="1.0">
//  <sdl:seg-defs>
//      <sdl:seg id="1" conf="Translated">
//          <sdl:prev-origin origin="source">
//              <sdl:prev-origin origin="source">
//                  <sdl:prev-origin origin="tm" percent="99">
//                      <sdl:prev-origin/>
//                      <sdl:value key="Hash">Foo1</sdl:value>
//                      <sdl:value key="Created">Boo1</sdl:value>
//...
//..
//.

I've tried like this but, it failed.

string myResult = "";
XDocument myDoc = XDocument.Load(myPath);
XNamespace myNS = "http://sdl.com/FileTypes/SdlXliff/1.0";
foreach (var x in myDoc.Descendants(myNS + "seg-defs"))
    myResult += x.Value.ToString() + "\n";
MessageBox.Show(myResult);

Following are not what I wanted..

//  Foo1Boo1
//  Foo2Boo2
//  ....
//  ..
//  .

Help, please.

Thanks

Upvotes: 0

Views: 37

Answers (1)

jdweng
jdweng

Reputation: 34421

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> segments = doc.Descendants().Where(x => x.Name.LocalName == "seg").ToList();

            List<XElement> created = segments.Descendants().Where(x => (x.Name.LocalName == "value") && ((string)x.Attribute("key") == "Created")).ToList();

            string results = string.Join("\n", created.Select(x => (string)x));
        }
    }
}

Upvotes: 1

Related Questions