在 C# 中使用 LINQ 将选定的 XML 元素添加到 DataGrid



我有来自REST服务的XML响应。我试图展示它如何具有深层元素结构。如您所见,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 响应中获取 20 个元素值到我的 DataGridView。

我尝试使用此绑定使用数据表/数据集,但无法访问具有相同名称的子元素。它采用第一个匹配元素。

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>

看看这是否有效:

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:temptest.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();

        }
    }
}

最新更新