Reputation: 1933
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?
Upvotes: 0
Views: 176
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