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 8109693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:29:54+00:00 2026-06-06T01:29:54+00:00

In a program that changes settings in a .csproj file, the following linq to

  • 0

In a program that changes settings in a .csproj file, the following linq to xml statement always returns “object not set to an instance of an object”:

static void Main(string[] args)
    {
        string rootDir =
            @"e:\path\to\proj\file\foo.csproj";

        var xDoc = XDocument.Load(rootDir);
        var ns = xDoc.Root.Name.Namespace;

        var hasConditions = xDoc.Root.Elements(ns + "PropertyGroup")
            .Where(x => x.Attribute("Condition") != null);

        Console.WriteLine(hasConditions.Count());

        try
        {
            var debugConfig = xDoc.Root.Elements(ns + "PropertyGroup")
                .Where(x => x.Attribute("Condition")
                .Value == " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");

            Console.WriteLine(debugConfig.Count());
        }
        catch
        {
            Console.WriteLine("doesn't work");
        }
    }

Complete proj file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>8.0.30703</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{F08E2AEB-A1C2-43F9-A93C-38AF2A99C96A}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>CommunicationSystem.XmlLoading.Common</RootNamespace>
    <AssemblyName>CommunicationSystem.XmlLoading.Common</AssemblyName>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <SccProjectName>SAK</SccProjectName>
    <SccLocalPath>SAK</SccLocalPath>
    <SccAuxPath>SAK</SccAuxPath>
    <SccProvider>SAK</SccProvider>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <RunCodeAnalysis>true</RunCodeAnalysis>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <RunCodeAnalysis>true</RunCodeAnalysis>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DEV|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\DEV\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'SIT|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\SIT\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'QA|AnyCPU'">
    <OutputPath>bin\QA\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="StorageSystem">
      <HintPath>..\..\..\GlobalDependencies\StorageSystem\beta\StorageSystem.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Core" />
    <Reference Include="System.Web" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Common\IEnumerableExtensions.cs" />
    <Compile Include="Common\XmlLoaderBase.cs" />
    <Compile Include="Configuration\SchemasConfigurationElement.cs" />
    <Compile Include="Configuration\SettingsConfigurationElement.cs" />
    <Compile Include="Configuration\TConfigurationElementCollection.cs" />
    <Compile Include="Configuration\XmlLoadingConfigurationSection.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Validation\XValidationResult.cs" />
    <Compile Include="Validation\XValidator.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
   Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

My suspicion is that it has something to do with the whitespace in the attribute value, but I can’t be sure…. Anyone else face something similar?

  • 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-06-06T01:29:56+00:00Added an answer on June 6, 2026 at 1:29 am

    I suspect this is the problem:

    x.Attribute(ns + "Condition")
    

    Are you sure the attribute is in a namespace? (Unlike element names, attribute names don’t inherit a “default” namespace from xmlns="...".) Try just:

    x.Attribute("Condition")
    

    EDIT: If the problem is indeed that the attribute doesn’t exist on all elements, then the solution is simpler than the one proposed by arcain. You can just use a cast to string:

    var debugConfig = xDoc.Root.Elements(ns + "PropertyGroup").Where(x => 
          (string) x.Attribute("Condition") == " [long string here] ");
    

    The string conversion will return null when called on null, which is what you want.

    (Apologies for the messy formatting – the short lines on SO make it tricky.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application have a configuration xml-file. That file contains more than 50 program settings.
I have a program that reads one image file, makes some changes on that
I have a program that depends on a configuration file for three settings. I
I'm writing a Python program that, if the user changes settings while running it,
I'm writing a program that listens to the System Clipboard for changes. The listener
I have a C++ program that crashed due to a bug. It would not
My program has a handful of settings that need to be established before it
I have a swf (flash) program that I projected to an exe file. I
So I am working on a C# program that takes in a set of
When I try to do that I get the following error: Changes to 64-bit

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.