简体   繁体   中英

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:

在此处输入图片说明

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?

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);
}

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM