Any help would be appreciated for the following problem. I've already
spent a day at this, and enough is enough.
I'm writing a ORM program. and in the process of running it against a
database, I'm creating dozens of classes. I want to insert these
classes into the .csproj file automatically. I can do it just fine
manually, and I can do it also programatically up to the point that I
save the XmlDocument object. At that point, for some Redmond reason,
the XmlDocument.Save method insists on writing an xmlns="" attribute
to the node. This causes Visual Studio to puke upon trying to load the
project.
Here's the code that I use to add the node programatically
public Form1()
{
InitializeComponent();
XmlDocument doc = new XmlDocument();
string filename =
@"e:\Projects.NET\BB\BusinessBuilder.csproj";
doc.Load(filename);
XmlNode currentnode = doc.DocumentElement.FirstChild;
Boolean test = false;
do
{
currentnode = currentnode.NextSibling;
if (currentnode == null)
break;
if (currentnode.Name == "ItemGroup" &&
currentnode.FirstChild.Name == "Compile")
test = true;
} while (test == false);
if (test == true)
{
XmlElement elem = doc.CreateElement("Compile");
XmlAttribute att = doc.CreateAttribute("Include");
att.Value = @"DAL\test.cs";
elem.Attributes.Append(att);
currentnode.AppendChild(elem);
}
doc.Save(filename);
}
after the program runs, here is the line that it has inserted (in the
proper place)
<Compile Include="DAL\test.cs" xmlns="" />
and here is the error message that I receive when I try to load the
project
Unable to read the project file 'BusinessBuilder.csproj',
E:\Projects.NET\BB\BusinessBuilder.csproj (105,5):
The element <Compile> beneath element <ItemGroup> may
not have a custom XML namespace.
If I remove the xmlns="" manually, the project then opens fine.
All I can assume from my testing is that Microsoft must have something
else it uses to write to this file, since they apparently have no
problem doing the same thing if I add the class through the Solution
Explorer. If I can't get an answer, I'm going to be forced to treat
this thing as a plain text file, and add the lines in that way. That's
a hell of a way to deal with 'state-of-the-art' technology.
At any rate, any help would be appreciated. TIA.
Bjoern Hoehrmann - 17 Mar 2007 05:36 GMT
* Al Pilon wrote in microsoft.public.dotnet.xml:
>I'm writing a ORM program. and in the process of running it against a
>database, I'm creating dozens of classes. I want to insert these
[quoted text clipped - 4 lines]
>to the node. This causes Visual Studio to puke upon trying to load the
>project.
An xmlns='' attribute undeclares the current in-scope default namespace
declaration, they are created automatically if you create elements in no
namespace and add them to an element with a default namespace, for
example. Instead of
> XmlElement elem = doc.CreateElement("Compile");
This, you have to use one of the multi-argument overloads and specify
the namespace of the element (look at the xmlns='' attributes on the
ancestors of the element you are adding to find out which the right one
is, I am not sure what it is for your version of VS.NET). The MSDN
documentation for CreateElement has examples for this.

Signature
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Al Pilon - 17 Mar 2007 06:36 GMT
Thanks for the quick response. It works fine now. Appreciate it.
>* Al Pilon wrote in microsoft.public.dotnet.xml:
>>I'm writing a ORM program. and in the process of running it against a
[quoted text clipped - 18 lines]
>is, I am not sure what it is for your version of VS.NET). The MSDN
>documentation for CreateElement has examples for this.