2017-10-29 8 views
0

REST 서비스의 XML 응답이 있습니다. 나는 그것이 깊은 요소 구조를 가지고 있음을 보여 주려고 노력했다. 보시다시피 XML에는 동일한 이름의 하위 요소가 있습니다.C#에서 LINQ를 사용하여 DataGrid에 선택된 XML 요소

<list> 
<message/> 
<message/> 
<message/> 
<message> 
    <messageId>3</messageId> 
    <serverId>f5890d03-0bef-4704-a9de-9a1be64801c0</serverId> 
    <channelId>ffffffff-3656-4b6f-8fd1-253e99177b80</channelId> 
    <receivedDate> 
     <time>1509259172350</time> 
     <timezone>GMT+03:00</timezone> 
    </receivedDate> 
    <processed>true</processed> 
    <connectorMessages class="linked-hash-map"> 
     <entry> 
      <int>0</int> 
      <connectorMessage> 
       <messageId>3</messageId> 
       <metaDataId>0</metaDataId> 
       <channelId>ffffffff-3656-4b6f-8fd1-253e99177b80</channelId> 
       <raw> 
        <encrypted>false</encrypted> 
        <channelId>ffffffff-3656-4b6f-8fd1-253e99177b80</channelId> 
        <messageId>3</messageId> 
        <metaDataId>0</metaDataId> 
        <contentType>RAW</contentType> 
       </raw> 
       <response> 
        <encrypted>false</encrypted> 
        <channelId>ffffffff-3656-4b6f-8fd1-253e99177b80</channelId> 
        <messageId>3</messageId> 
        <metaDataId>0</metaDataId> 
       </response> 
       <sourceMapContent> 
        <encrypted>false</encrypted> 
        <content class="java.util.Collections$UnmodifiableMap"> 
         <m> 
          <entry> 
           <string>remoteAddress</string> 
           <string>127.0.0.1</string> 
          </entry> 
          <entry> 
           <string>destinationSet</string> 
           <linked-hash-set> 
            <int>1</int> 
           </linked-hash-set> 
          </entry> 
         </m> 
        </content> 
       </sourceMapContent> 
       <connectorMapContent> 
        <encrypted>false</encrypted> 
        <content class="map"> 
         <entry> 
          <string>_source</string> 
          <string>Instance1</string> 
         </entry> 
         <entry> 
          <string>_version</string> 
          <string>2.5.1</string> 
         </entry> 
        </content> 
       </connectorMapContent> 
       <encrypted>false</encrypted> 
       <errorCode>0</errorCode> 
      </connectorMessage> 
     </entry> 
    </connectorMessages> 
</message> 
<message/> 
</list> 

저는 C# 및 C# XML 프로세스에서 새로 도입되었습니다. 주말에 DataGridView에 바인딩하는 방법을 찾아 보았습니다. 이 XML 응답에서 DataGridView로 20 요소 값만 가져 가고 싶습니다.

이 바인딩에 대해 DataTable/DataSet을 사용했지만 동일한 이름을 가진 자식 요소에 연결할 수 없습니다. 첫 번째로 일치하는 요소를 사용합니다.

var table = new DataTable("message"); 
table.Columns.Add("messageId", typeof(string)); 
table.Columns.Add("serverId", typeof(string)); 
table.Columns.Add("time", typeof(string)); 
table.Columns.Add("receivedDate", typeof(string)); 
table.Columns.Add("timezone", typeof(string)); 
table.ReadXml(new StringReader(contentSystemInfo)); 
dataGridView1.DataSource = table; 

나는 LINQ를 시도했습니다.

XDocument xmlDocument = XDocument.Parse(contentSystemInfo); 
List<string> messageRAWContentList = xmlDocument.Root 
    .Elements("message") 
    .Elements("connectorMessages") 
    .Elements("entry") 
    .Elements("connectorMessage") 
    .Elements("raw") 
    .Elements("content") 
    .Select(x => (string)x) 
    .ToList(); 

여전히 하위 요소에 도달 할 수 없습니다.

예를 들어 모든 메시지 요소에서이 요소에 도달하려고합니다.

<list> 
<message> 
    <messageId>3</messageId> 
    <serverId>f5890d03-0bef-4704-a9de-9a1be64801c0</serverId> 
    <receivedDate> 
     <time>1509259172350</time> 
    </receivedDate> 
    <connectorMessages class="linked-hash-map"> 
     <entry> 
      <messageId>13</messageId> 
      <connectorMessage> 
       <raw> 
        <content>content</content> 
       </raw> 
       <response> 
        <content>content</content> 
       </response> 
       <sourceMapContent> 
        <content> 
         <m> 
          <entry> 
           <string>remoteAddress</string> 
           <string>127.0.0.1</string> 
          </entry> 
          <entry> 
           <string>destinationSet</string> 
           <linked-hash-set> 
            <int>1</int> 
           </linked-hash-set> 
          </entry> 
         </m> 
        </content> 
       </sourceMapContent> 
      </connectorMessage> 
     </entry> 
    </connectorMessages> 
</message> 
</list> 

답변

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

      var results = doc.Descendants("message").Select(x => new { 
       messageId = (int)x.Element("messageId"), 
       serverId = (string)x.Element("serverId"), 
       channelId = (string)x.Element("channelId"), 
       time = (long)x.Descendants("time").FirstOrDefault(), 
       connectorMessages = x.Descendants("connectorMessage").Select(y => new { 
        messageId = (int)y.Descendants("messageId").FirstOrDefault(), 
        raw = y.Elements("raw").Select(z => new { 
         encrypted = (bool)z.Element("encrypted"), 
         channelId = (string)z.Element("channelId"), 
         messagedId = (int)z.Element("messageId"), 
         metaDataId = (string)z.Element("metaDataId"), 
         contentType = (string)z.Element("contentType") 
        }).FirstOrDefault(), 
        response = y.Elements("response").Select(z => new { 
         encrypted = (bool)z.Element("encrypted"), 
         channelId = (string)z.Element("channelId"), 
         messagedId = (int)z.Element("messageId"), 
         metaDataId = (string)z.Element("metaDataId") 
        }).FirstOrDefault(), 
        entries = y.Descendants("entry").Select(z => new { 
          strings = z.Elements("string").Select(b => (string)b).ToList() 
        }).ToList() 
       }).ToList() 
      }).ToList(); 


     } 
    } 
} 
+0

을 일했다! 대단히 감사합니다 @ jdweng – Erdogan