1teamsah
1teamsah

Reputation: 1933

How to get text from MS Project's XML file in C#?

I've an XML file that created in MS Project. It is like as that:

enter image description here

I want to get UID values in "Resource" Node. I try this:

var xmlDoc = new XmlDocument();
        string strFileName = "Sample.xml";
        xmlDoc.Load(strFileName);

        XmlNodeList xnList = xmlDoc.SelectNodes("/Project/Resources/Resource");
        foreach (XmlNode xn in xnList)
        {

            Console.WriteLine(xn["UID"].InnerText);
        }

However, xmlDoc.SelectNodes("/Project/Resources/Resource"); returns nothing. What is wrong?

Upvotes: 0

Views: 176

Answers (2)

Alexander Petrov
Alexander Petrov

Reputation: 14231

You must add namespace:

var man = new XmlNamespaceManager(xmlDoc.NameTable);
man.AddNamespace("ns", "http://schemas.microsoft.com/project");

XmlNodeList xnList = xmlDoc.SelectNodes("/ns:Project/ns:Resources/ns:Resource", man);

Also, you can do it as:

var xnList = xmlDoc.SelectNodes("/ns:Project/ns:Resources/ns:Resource/ns:UID", man);

foreach (XmlNode xn in xnList)
{
    Console.WriteLine(xn.InnerText);
}

Upvotes: 1

Jon Iles
Jon Iles

Reputation: 2579

MSPDI is not a pleasant XML format to work with. You may find the C# variant of MPXJ easier to work with to extract data. You'll find it on NuGet!

Upvotes: 0

Related Questions