tags:

views:

132

answers:

2

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

A: 

Use name.Value.

Femaref
yes that works too. thank you
Dula
+1  A: 

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
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
@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