【问题标题】:XElement parsing from XDocument, far below, repeated从 XDocument 解析 XElement,远低于,重复
【发布时间】:2017-05-31 01:57:28
【问题描述】:

我想让我的结果是这样的;

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

从这里..

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

我试过这样,但是失败了。

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

以下不是我想要的..

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

请帮忙。

谢谢

【问题讨论】:

    标签: c# xml parsing


    【解决方案1】:

    尝试以下:

    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));
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-03
      • 1970-01-01
      • 2010-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      相关资源
      最近更新 更多