3

In Visual Studio -> Debug -> Command line arguments enter the following XML.

"<AppParameters><ConnectionString>Server=localhost;Database=MyDB;User Id=sa;Password=YouNameIt</ConnectionString></AppParameters>"

Then you read this with the following lines

DataSet parameter = new DataSet();
parameter.ReadXml(new StringReader(xmlParameter));

All works great!

Then you restart Visual Studio and after starting your project, you will get this error:

An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll

The first time, of course, you will spend some time to investigate what happend. First suggestion of course, your fault. But then you'll see this:

"<AppParameters xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><ConnectionString>Server=localhost;Database=MyDB;User Id=sa;Password=YouNameIt</ConnectionString></AppParameters>"

Visual Studio has changed the command line arguments.

Why?!

  • 1
    Any reason for not using the App.config/Web.config file for the project to store connection strings? – Jontatas Jul 02 '14 at 13:04
  • Are you **sure** you can use such special characters in command line? Even if quoted? – Adriano Repetti Jul 02 '14 at 13:05
  • That's not the type of thing you pass at the command line typically, so if you have problems doing so I'm not surprised. As far as why it was changed: it's probably because VS wrote the XML to save it, and the XML persistence code is written to add the MSBuild namespace to the XML. – Ken White Jul 02 '14 at 13:06
  • @Jontatas: Because it must be passed to the application dynamically before runtime. – user3797717 Jul 09 '14 at 08:30
  • @Adriano Repetti: Yes, I have tested it in production, and it's working perfect. – user3797717 Jul 09 '14 at 08:33
  • @Ken White: Please think about first, what I am trying here, before answering anything else. I do not have problems with that, so there are no surprises at runtime at all. The only surprise is, that Visual Studio can not handle it for some reasons, but DataSet can. And do not mention just something! When using DataSet.WriteXml it does not change the xml at all, so why does Visual Studio? – user3797717 Jul 09 '14 at 08:34
  • @user3797717 first of all we're all here trying to help so please **be kind**. For second I repeat my question: are you sure you can use such special characters in command line (within Visual Studio)? Answer is **no** because it'll do two things: kindly escape `<` and `>` with command line rules with `^` (and you can't omit quotes because of spaces in your text). Second and most important using quotes you bypass XML escaping it uses (try by yourself). – Adriano Repetti Jul 09 '14 at 10:35
  • You _may_ manually edit .csproj.user to enclose it in a `<![CDATA[` section (it'll work until next change in properties) or do not pass XML as command line argument (as @KenWhite suggested). Usually XML is _"...not the type of thing you pass at command line typically"_ then if you have problems do not do it. Is it something generated by a program? Use a response file (or redirect stdin). Is it something entered by user? Use true command line options. If VS has this bug it means they didn't find it in 12 years, there should be a reason for this... – Adriano Repetti Jul 09 '14 at 10:39

1 Answers1

0

I'm debugging a Python module and had the same problem. For more than half hour I went crazy, but finally I've got it. Notes:

  • I am using VS2k10.

  • The Debugging related settings are stored in the vcxproj.user file (I am going to refer to it as VUF).

While having the solution open, I started by removing VUF. Opening the Project Property Pages dialog, the default values are there:

  • Command: $(TargetPath)

  • CommandArguments:

In the same time VUF was generated (I didn't even have to close the dialog). Here's it's content (notice the namespace):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>

Then I populated the above fields (don't mind the values):

  • Command: E:\WinOBT\1.0.0.0\OPSWpython\2.7.10\x64_d\python_d.exe

  • CommandArguments: -c "import _xmlrpc as _x;print _x.decode('<value><string>aaa</string></value>')"

After closing the dialog (or pressing Apply), VUF content became:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
        <LocalDebuggerCommand>E:\WinOBT\1.0.0.0\OPSWpython\2.7.10\x64_d\python_d.exe</LocalDebuggerCommand>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
        <LocalDebuggerCommandArguments>-c "import _xmlrpc as _x;print _x.decode('<value><string>aaa</string></value>')"</LocalDebuggerCommandArguments>
        <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
    </PropertyGroup>
</Project>

while CommandArguments became: -c "import _xmlrpc as _x;print _x.decode('<value xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><string>aaa</string></value>')"

VS as we know, stores its settings in xml format (not the string representation in the file, but the actual xml tree - that results from parsing the string representation). Well, I've learned that parsing an xml string (that converts it to a tree), then converting back the tree to string, will yield (in most of the cases) a different result than the original string.

Here I described this behavior (well it's about a Python parser, but I'm sure that all - or many - behave the same).

So, we have an xml (that we would like to be stored as plain text) inside an xml (the project settings), and that confuses a little VS's parser when it attempts to parse it, yielding a behavior that I think it's odd:

  • if it succeeds:

    • it will add the xmlns attribute, and therefore messing up the original xml text in the dialog box, and also when passing to the program
    • it will store it as we passed it in VUF
  • if it fails for some reason (e.g. xml is invalid):

    • it will store as we passed it in the dialog box (and it will properly pass it as an argument to the executable being debugged)
    • it will escape the xml special chars in VUF

After I circled around i found a way to partially overcome this (it's a lame workaround - or gainarie):

  • manually modify VUF, turning our xml into a CDATA section:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
            <LocalDebuggerCommand>E:\WinOBT\1.0.0.0\OPSWpython\2.7.10\x64_d\python_d.exe</LocalDebuggerCommand>
        </PropertyGroup>
        <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
            <LocalDebuggerCommandArguments><![CDATA[-c "import _xmlrpc as _x;print _x.decode('<value><string>aaa</string></value>')"]]></LocalDebuggerCommandArguments>
        <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
            <LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
        </PropertyGroup>
    </Project>
    
  • Reload the solution (as VS doesn't read VUF every time we open the dialog since its internal tree hasn't changed)

Now the xml string is properly passed to the executable, and also properly displayed in the Project Property Pages dialog. Beware: if you modify any of the settings, in the dialog, that will trigger VS to reload (reparse) the data, and it will mess it up again.

CristiFati
  • 38,250
  • 9
  • 50
  • 87