2017-10-29 5 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

関連する問題