简体   繁体   中英

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.

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:

        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++;
                }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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