2016-12-05 3 views
2

私がここでやっていることは、omnipage xmlをalto xmlに変換することです。だから私はC#を使うことに決めました。C#LinQからXMLへの出力を作成

そして、ここでは私のサンプルXMLファイル

<wd l="821" t="283" r="1363" b="394"> 
<ch l="821" t="312" r="878" b="394" conf="158">n</ch> 
<ch l="888" t="312" r="950" b="394" conf="158">o</ch> 
<ch l="955" t="283" r="979" b="394" conf="158">i</ch> 
<ch l="989" t="312" r="1046" b="394" conf="158">e</ch> 
<ch l="1051" t="312" r="1147" b="394" conf="158">m</ch> 
<ch l="1157" t="283" r="1219" b="394" conf="158">b</ch> 
<ch l="1224" t="312" r="1267" b="394" conf="198">r</ch> 
<ch l="1267" t="283" r="1296" b="394" conf="198">i</ch> 
<ch l="1306" t="312" r="1363" b="394" conf="158">e</ch> 
</wd> 

されており、ここにいる私のコード私は上記のように、単純なXMLを使用する際に私の質問がある

XDocument document = XDocument.Load(fileName); 
var coordinates = from r in document.Descendants("wd").ToList().Where 
        (r => (string)r.Attribute("l") != "") 
        select new 
        { 
         left = r.Attribute("l").Value, 
        }; 

foreach (var item in coordinates) 
{ 
    Console.WriteLine(item.left); 
} 
Console.ReadLine(); 

が、それは動作しますが、ときに私をこのような長いXMLをリンクで使用してください。

http://pastebin.com/LmDHRzC5 

これは機能しません。

しかし、wdタグもあり、L属性も持っています。

ありがとうございます。長すぎるために長いXMLをペーストビンに貼り付けます。

+0

「動作しません」とはどういう意味ですか?エラーがありますか?はいの場合はどちらですか? – Sefe

答えて

2

あなたは

<document xmlns="http://www.scansoft.com/omnipage/xml/ssdoc-schema3.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

次作品

document.Descendants().Where(e => e.Name.LocalName == "wd") 

それとも、私はすべてやるつもりはないSearch XDocument using LINQ without knowing the namespace

+0

ありがとうございました。出来た。 –

+0

私は座標ループ foreachの(座標でのvarアイテム) {私はここ が何をする必要がありますどのような //} –

+0

@pdftoimageに内部にある各CHのタグの内容を取得する必要があります別の質問を持っています(n => n.Value).ToArray() 'を選択してから、それらを出力してください(' e => e.Name.LocalName == "ch")。 、 "Console.WriteLine(item.left +": "+ String.Join("、 "、item.chs));' –

1

から別のオプションを使用することができ、あなたの大きな文書の名前空間を持っていますコードがこれを開始する必要があります。私はxml linqを使用しました

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      StreamReader reader = new StreamReader(FILENAME); 
      //skip xml identification with UTF-16 
      reader.ReadLine(); 
      XDocument doc = XDocument.Load(reader); 

      XElement body = doc.Descendants().Where(x => x.Name.LocalName == "body").FirstOrDefault(); 
      XNamespace ns = body.GetDefaultNamespace(); 

      var results = new { 
       sections = body.Elements(ns + "section").Select(x => new { 
        l = (int)x.Attribute("l"), 
        r = (int)x.Attribute("r"), 
        b = (int)x.Attribute("b"), 
        runs = x.Descendants(ns + "run").Select(y => new { 
         wds = y.Elements(ns + "wd").Select(z => new { 
          chs = z.Elements(ns + "ch").Select(a => new { 
           l = (int?)a.Attribute("l"), 
           t = (int?)a.Attribute("t"), 
           r = (int?)a.Attribute("r"), 
           b = (int?)a.Attribute("b"), 
           conf = (int?)a.Attribute("conf"), 
           value = (string)a 
          }).ToList() 
         }).ToList() 
        }).ToList() 
       }).ToList(), 
       dds = body.Elements(ns + "dd").Select(x => new { 
        l = (int)x.Attribute("l"), 
        r = (int)x.Attribute("r"), 
        b = (int)x.Attribute("b"), 
        paras = x.Elements(ns + "para").Select(y => new { 
         lns = y.Elements(ns + "ln").Select(z => new { 
          wds = z.Elements(ns + "wd").Select(a => new { 
           chs = a.Elements(ns + "ch").Select(b => new { 
            l = (int?)b.Attribute("l"), 
            t = (int?)b.Attribute("t"), 
            r = (int?)b.Attribute("r"), 
            b = (int?)b.Attribute("b"), 
            conf = (int?)b.Attribute("conf"), 
            value = (string)b 
           }).ToList() 
          }).ToList() 
         }).ToList() 
        }).ToList() 
       }).ToList(), 

      }; 
     } 
    } 
} 
関連する問題