Reputation: 389
I have a xml file like below, i want to find the node which property "name" value equals "ImageListView"
I have writen below code:
var nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1");
xpath = "//asmv1:assembly/dependency/dependentAssembly/assemblyIdentity[name='ImageListView']";
XElement ele = doc.XPathSelectElement(xpath, nsmgr);
ele.Remove();
but cannot find anything. is any wrong here? thanks.
Upvotes: 2
Views: 308
Reputation: 89285
Your XML has default namespace here :
<asmv1:assembly
......
xmlns="urn:schemas-microsoft-com:asm.v2"
......>
Therefore, all XML element without prefix considered in default namespace. You need to add prefix that point to default namespace URI, and use it in XPath :
var nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1");
nsmgr.AddNamespace("d", "urn:schemas-microsoft-com:asm.v2");
xpath = "//asmv1:assembly/d:dependency/d:dependentAssembly/d:assemblyIdentity[@name='ImageListView']";
XElement ele = doc.XPathSelectElement(xpath, nsmgr);
ele.Remove();
UPDATE :
fixed the XPath slightly. You need to use @
to point to attribute : ... [@name='ImageListView']
Upvotes: 3