2016-12-05 1 views
2

내가 여기서하고있는 것은 옴니 페이지 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을 pastebin에 붙여 넣습니다.

+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

은 내가 (좌표 VAR 항목) 좌표 루프 foreach 문 안에있는 각 채널 태그의 내용을 얻을 필요가 { // 내가 여기 할 필요가 무엇}에 –

+0

@pdftoimage을 (n => n.Value) .ToArray()'를 선택하고 그 값을 출력한다. (ch = r.Descendants(), 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(), 

      }; 
     } 
    } 
} 
관련 문제