Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2007

Tip: Looking for answers? Try searching our database.

Saving .csproj files

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Al Pilon - 17 Mar 2007 03:03 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.