选择XDOC查询语句 [英] Select statement for xdoc query

查看:54
本文介绍了选择XDOC查询语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在xml语句的Messages中添加一个子类别,有没有办法做到这一点?GroupMessages -> Message -> GroupMessage:

I am trying to add a sub category to Messages in my xml statement Is there a way I can do this GroupMessages -> Message -> GroupMessage :

        var groups = xDoc.Descendants("Group")
            .Select(n => new
            {
                GroupName = n.Element("GroupName").Value,
                GroupHeader = n.Element("GroupHeader").Value,
                TimeCreated = DateTime.Parse(n.Element("TimeAdded").Value),
                Tags = n.Element("Tags").Value, 
                Messages = n.Element("GroupMessages").Value
                //line above
            })
            .ToList();
        dataGrid2.ItemsSource = groups;

在我的方法中,GroupMessages包含MessageID和GroupMessage,并且在一个容器中的datagrid中都列出了它们.因此,我尝试了此操作,但未列出任何内容:

In my method GroupMessages contains both MessageID and GroupMessage and it is listing both in my datagrid within the one container. So I tried this but it lists nothing:

 Messages = n.Descendants("GroupMessages").Select(nd => nd.Element("GroupMessage").Value)

我的XML如下:

<Group>
<TimeAdded>2012-04-27T10:23:50.7153613+01:00</TimeAdded>
<GroupName>Group</GroupName>
<GroupHeader>Header</GroupHeader>
<GroupMessages>
<Message>
<MessageID>1</MessageID>
<GroupMessage>Message</GroupMessage>
<MessageGroup/>
</Message>
</GroupMessages>
</Group>

我也尝试过:

Messages = n.Descendants("GroupMessages").Select(nd => nd.Descendants("Message").Select(nde => nde.Element("GroupMessage").Value))

无济于事?

更新:

    private void ListGroups_Click(object sender, RoutedEventArgs e)
    {
        string uriGroup = "http://localhost:8000/Service/Group";
        XDocument xDoc = XDocument.Load(uriGroup);
        var groups = xDoc.Descendants("Group")
                    .Select(n => new
        {
            GroupName = n.Element("GroupName").Value,
            GroupHeader = n.Element("GroupHeader").Value,
            TimeCreated = n.Element("TimeAdded").Value,
            Tags = n.Element("Tags").Value,
            Messages = n.Element("GroupMessages").Descendants("Message").Select(nd => new
            {
                //Id = nd.Element("MessageID").Value,
                Message = nd.Element("GroupMessage").Value
            }).FirstOrDefault()
        })
        .ToList();
        dataGrid2.ItemsSource = groups;
    }

Unfortunatley,此方法在数据网格的单元格内显示集合".如果我尝试使用ToArray,它将在单元格内显示一个数组消息.有没有一种实际显示GroupMessage的方法?不确定如何设置数据网格的子元素?

Unfortunatley this method shows "Collection" inside the cell in the datagrid. If I try ToArray it will show an array message inside the cell. Is there a way to actually display the GroupMessage? Not sure how you set the child elements of a datagrid?

推荐答案

在最基本的级别上,您可以执行此操作以获取单个消息(第一个消息):

At the most basic level, you can do this to get a single message (the first one):

var groups = from grp in xDoc.Descendants("Group")
             select new { 
                GroupName = grp.Element("GroupName").Value,
                GroupHeader = grp.Element("GroupHeader").Value,
                TimeCreated = DateTime.Parse(grp.Element("TimeAdded").Value),
                Message = grp.Element("GroupMessages").Element("Message").Element("GroupMessage").Value
             };

但是,我假设您希望Messages是ID和Message都为消息的列表.在这种情况下,请考虑以下问题:

However, I assume that you want Messages to be a list of messages with both ID and Message. In that case, consider this:

var groups = from grp in xDoc.Descendants("Group")
             select new { 
                GroupName = grp.Element("GroupName").Value,
                GroupHeader = grp.Element("GroupHeader").Value,
                TimeCreated = DateTime.Parse(grp.Element("TimeAdded").Value),
                Messages = grp.Element("GroupMessages")
                             .Descendants("Message")
                             .Select(msg => new { 
                                 Id = msg.Element("MessageID").Value, 
                                 Message = msg.Element("GroupMessage").Value
                             }).ToList()
             };

但是,我强烈强调,匿名类的所有这种使用只会引起混乱.如果您有GroupMessage的类,请使用这些类.

However, I strongly stress that all this usage of anonymous classes is just going to cause confusion. If you have a class for Group and Message then use those.

请注意,您遇到的问题是忽略XML结构并选择随机元素.为了从单个元素中获取值,您将需要精确选择该元素,并要求输入.Value.仅选择它的父项,还是选择父项的父项(如您所做的)是不够的.

Note that the problem you're having is you're ignoring the XML structure and selecting random elements. To get the value out of a single element, you're going to need to select exactly that element, and ask for .Value. Selecting it's parent, or it's parent's parent (as you did) is not enough.

这篇关于选择XDOC查询语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆