Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中使用LINQ将选定的XML元素添加到DataGrid#_C#_Xml_Linq_Datagridview_Datatable - Fatal编程技术网

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

C# 在C中使用LINQ将选定的XML元素添加到DataGrid#,c#,xml,linq,datagridview,datatable,C#,Xml,Linq,Datagridview,Datatable,我有来自REST服务的XML响应。我试图展示它是如何具有深层元素结构的。如您所见,XML具有同名的子元素 <list> <message/> <message/> <message/> <message> <messageId>3</messageId> <serverId>f5890d03-0bef-4704-a9de-9a1be64801c0</serverId>

我有来自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>
比我和林克试过的要多

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();
XDocument xmlDocument=XDocument.Parse(contentSystemInfo);
List messageRAWContentList=xmlDocument.Root
.要素(“信息”)
.元素(“connectorMessages”)
.要素(“条目”)
.元素(“connectorMessage”)
.元素(“原始”)
.要素(“内容”)
.选择(x=>(字符串)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>

3.
f5890d03-0bef-4704-a9de-9a1be64801c0
1509259172350
13
内容
内容
远程地址
127.0.0.1
注定的开始
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();


        }
    }
}

工作!非常感谢@jdweng
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();


        }
    }
}