Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7409227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:03:02+00:00 2026-05-29T06:03:02+00:00

In the linq to xml query below, I have 2 properties that are a

  • 0

In the linq to xml query below, I have 2 properties that are a list<string>, DefaultValues and Values.

If either of these elements are empty, I would like to set that property of the LiteValueParameter object to a new, empty list:

Values = new List<string>();

Instead, the linq query is giving me something lke this:

Values = new List<string>();
Values.Add("");

Is there any way to prevent an empty item being added to the list if I have an empty element in my XML?

Linq code:

//linq query
List<LiteValueParameter> valParams = new List<LiteValueParameter>();
valParams = (from c in doc.Descendants("Parameters").Descendants("Parameter")
             where (LiteParameterType)Enum.Parse(typeof(LiteParameterType), c.Element("ParameterType").Value, true) == LiteParameterType.Value
             select new LiteValueParameter()
             {
                 Id = c.Attribute("Id").Value,
                 DataType = Type.GetType(c.Element("DataType").Value, true),
                 DefaultValues = c.Elements("DefaultValues").Select(element => element.Value).ToList(),
                 DisplayText = c.Element("DisplayText").Value,
                 IsRequired = Convert.ToBoolean(c.Element("IsRequired").Value),
                 MinCount = Convert.ToInt32(c.Element("MinCount").Value),
                 MaxCount = Convert.ToInt32(c.Element("MaxCount").Value),
                 MinValue = c.Element("MinValue").Value,
                 MaxValue = c.Element("MaxValue").Value,
                 ParameterName = c.Element("ParameterName").Value,
                 Values = c.Elements("Values").Select(element => element.Value).ToList(),
                 ParameterType = (LiteParameterType)Enum.Parse(typeof(LiteParameterType), c.Element("ParameterType").Value, true),
                 DisplayType = c.Element("DisplayType").Value
             }).ToList();

XML Code:

<Parameters>
  <Parameter Id="PermissionList">
    <ParameterType>Value</ParameterType>
    <ParameterName>Permissions</ParameterName>
    <DisplayType>ListBox</DisplayType>
    <DisplayText>Permissions</DisplayText>
    <IsRequired>true</IsRequired>
    <MinValue />
    <MaxValue />
    <DefaultValues />
    <Values />
    <DataType>System.String</DataType>
    <MinCount>1</MinCount>
    <MaxCount>1</MaxCount>
  </Parameter>
</Parameters>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:03:03+00:00Added an answer on May 29, 2026 at 6:03 am

    I suppose you could work around it like this:

    DefaultValues = (c.Elements("DefaultValues").Count() == 1 && c.Elements("DefaultValues").First().Value == string.Empty) ? new List<string>() : 
    c.Elements("DefaultValues").Select(element => element.Value).ToList(),
    

    This approach feels very “hacky” though. Instead I would change your XML to have a DefaultValue element that you query instead:

    <DefaultValues>
      <DefaultValue>Foo</DefaultValue>
    </DefaultValues>
    

    This is much more natural and now you can just write your query like

    DefaultValues = c.Descendants("DefaultValue").Select(element => element.Value).ToList(),
    

    This will return an empty collection if you just have

    <DefaultValues/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a LINQ to XML query that does what I want, but
I have a Linq query below with 'product' containing sub-elements 'Name' and 'Price' in
My current LINQ query and example XML are below. What I'd like to do
I have a pretty simple Linq to XML query: var q = from c
I have the following XML LINQ query from my XDocument. var totals = (from
I'm having an issue getting a LINQ query to work. I have this XML:
I have a (disconnected) typed Dataset in an XML, which I query in LINQ:
I have a LINQ query from an XML file. I am filtering out certain
I have a List<Item> collection that I am trying to generate an xml file
I'm trying to construct a linq query that pulls all nodes that have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.