简体   繁体   English

删除空的XML元素

[英]Remove empty XML elements

I have an XML document that I need to databind, but first I need to remove all elements with an empty value. 我有一个需要数据绑定的XML文档,但是首先我需要删除所有带有空值的元素。

Previously, I did this: 以前,我这样做:

            IEnumerable<Message> data = from info in xdoc.Descendants(tns + "sign")
                                         where info.Element(tns + "current-message").Value != ""
                                         select
                                             new Message(
                                                 info.Element(tns + "name").Value,
                                                 info.Element(tns + "current-message").Value);

            MessageList.DataContext = data;

I am attempting to do the same on Windows Phone 8. but am unsuccessful, my current code: 我正在尝试在Windows Phone 8上执行相同的操作,但当前代码未成功:

        foreach (var info in xdoc.Descendants(tns + "sign"))
        {
                Items.Add(new ItemViewModel()
                    {
                        ID = i.ToString(),
                        LineOne = info.Element(tns + "direction").Value,
                        LineTwo = info.Element(tns + "current-message").Value,
                        LineThree = info.Element(tns + "name").Value

                    });
                i++;
        }

How would I add a clause that ensures that any elements without a "current-message" are not in this databind? 我将如何添加一个子句以确保没有任何“当前消息”的元素不在此数据绑定中?

 foreach (var info in xdoc.Descendants(tns + "sign"))
        {
if(info.Element(tns + "current-message").Value != "")
                {
                Items.Add(new ItemViewModel()
                    {
                        ID = i.ToString(),
                        LineOne = info.Element(tns + "direction").Value,
                        LineTwo = info.Element(tns + "current-message").Value,
                        LineThree = info.Element(tns + "name").Value

                    });
                i++;
                }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM