1

We create the vcproj file with the makefileproj keyword so we can use our own build in VS. My question is, using C#, how do you read the "C++" from the following vcproj file:

<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
 ProjectType="Visual C++"
 Version="8.00"
 Name="TestCSProj"
 ProjectGUID="{840501C9-6AFE-8CD6-1146-84208624C0B0}"
 RootNamespace="TestCSProj"
 Keyword="MakeFileProj"
 >
 <Platforms>
  <Platform
   Name="x64"
  />
  <Platform
   Name="C++"     ===> I need to read "C++"
  />
 </Platforms>

I used XmlNode and got upto the second Platform:

String path = "C:\\blah\\TestCSProj\\TestCSProj\\TestCSProj.vcproj";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(fs);
XmlNodeList oldFiles = xmldoc.GetElementsByTagName("Platform");
XmlAttribute name = oldFiles[1].Attributes[0];
Console.WriteLine(name.Name);

This will print Name, but I need "C++". How do I read that?

Thank you very much in advance

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
Dula
  • 1,404
  • 1
  • 14
  • 29
  • why don't you simple debug it, place a breakpoint on the console.WriteLine() line and mousehover the name variable ... you will see all properties that you can have, and it's easy to find one with the output as "C++" ... – balexandre Jun 05 '10 at 19:17
  • You are aware that .vcproj no longer exist on VS2010? – Hans Passant Jun 05 '10 at 19:33
  • really? no vcproj file for VS2010? So do they substitute it with another file or take out the vcproj file all togeether? – Dula Jun 07 '10 at 17:59

2 Answers2

1

You can access the value of the attribute with the Value property:

Console.WriteLine(name.Value);

Or even better, get it by name instead of indexing, which is shorter and more reliable:

Console.WriteLine(((XmlElement)oldFiles[1]).GetAttribute("Name"));

The GetAttribute method directly returns the value as a string.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
  • when I try to do that it gives the following error: Error1 'System.Xml.XmlNode' does not contain a definition for 'GetAttribute' But I did find the following code works: Console.WriteLine(name.InnerText); – Dula Jun 07 '10 at 17:58
  • @Dulantha: Hum. It seems the Attributes collection is available but GetAttribute isn't. That's odd. I revised my code to add the necessary cast. – Matti Virkkunen Jun 07 '10 at 20:15
0

Use name.Value.

Femaref
  • 60,705
  • 7
  • 138
  • 176