I need to select a node which has attribute name as _1.1.1
I am trying to select the node as
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("IKS", "http://schemas.microsoft.com/wix/2006/objects");
XmlNode xRootNode = xmlDoc.SelectSingleNode("//folder[#name='Global']");
But it doesn't return any. I'm sure its because of the special characters in my expression. How should I handle it to get the desired node?
EDIT: I access node as
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("IKS", "http://schemas.microsoft.com/wix/2006/objects");
XmlNode xRootNode = xmlDoc.SelectSingleNode("//folder[#name='Global']", nsmgr);
and the XML is
<?xml version="1.0" encoding="UTF-8"?>
<workplace xmlns='IKS:'>
<cabinet name='Groups%20and%20Departments' oid='_1.25.18'>
<folder name='Global' oid='_1.11.9882'></folder>
</cabinet>
</workplace>
You're very close to having the correct approach. You have declared a namespace prefix, but you need to actually use it:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("IKS", "http://schemas.microsoft.com/wix/2006/objects");
XmlNode xRootNode = xmlDoc.SelectSingleNode("//IKS:folder[#name='Global']");
// ^^^^------- here
Note: For some reason, you have xmlns="IKS:" in your XML. If that is in fact what your XML looks like, then IKS: is the namespace URI you need to use:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("IKS", "IKS:");
XmlNode xRootNode = xmlDoc.SelectSingleNode("//IKS:folder[#name='Global']");
Related
I'm loading a string into an XML document that contains the following structure:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="clsWorker.cs" />
</ItemGroup>
</Project>
Then I'm loading all into an XmlDocument:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(Xml);
Then the following problem occurs:
XmlNode Node = xmldoc.SelectSingleNode("//Compile"); // returns null
When I remove the xmlns attribute from the root element (Project), it works fine.
How do I get SelectSingleNode to return the relevant element?
You should use an XmlNamespaceManager in your call to SelectSingleNode():
XmlNamespaceManager ns = new XmlNamespaceManager(xmldoc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNode node = xmldoc.SelectSingleNode("//msbld:Compile", ns);
Taken right from the documentation of SelectSingleNode() on the MSDN:
Note
If the XPath expression does not include a prefix, it is assumed that the
namespace URI is the empty namespace. If your XML includes a default
namespace, you must still add a prefix and namespace URI to the
XmlNamespaceManager; otherwise, you will not get a node selected. For
more information, see Select Nodes Using XPath Navigation.
And the immediately following sample code is
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);
It's not as if this would be "hidden knowledge". ;-)
This way you don't need to specify namespace:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("your xml");
XmlNode node = xmlDoc.SelectSingleNode("/*[local-name() = 'Compile']");
XmlNode nodeToImport = xmlDoc2.ImportNode(node, true);
xmlDoc2.AppendChild(nodeToImport);
Since the 'ItemGroup' may have multiple 'Compile' children, and you specifically want the 'Compile' children of 'Project/ItemGroup', the following will return all of the desired 'Compile' children and no others:
XmlDocument projectDoc = new XmlDocument();
projectDoc.Load(projectDocPath);
XmlNamespaceManager ns = new XmlNamespaceManager(projectDoc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNodeList xnList = projectDoc.SelectNodes(#"/msbld:Project/msbld:ItemGroup/msbld:Compile", ns);
Note that the 'msbld:' namespace specification needs to precede each node level.
Given this XML, how to select the value of PostBack using xpath query?
<AuthenticateResponse xmlns="http://example.com/authentication/response/1">
<Status>success</Status>
<Result>more-info</Result>
<StateContext />
<AuthenticationRequirements>
<PostBack>/Authentication/ExplicitForms/AuthenticateAttempt</PostBack>
<CancelPostBack>/Authentication/ExplicitForms/CancelAuthenticate</CancelPostBack>
<CancelButtonText>Cancel</CancelButtonText>
</AuthenticationRequirements>
</AuthenticateResponse>
I would expect this to work but it returns null:
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("AuthenticateResponse", "http://example.com/authentication/response/1");
var node = doc.SelectSingleNode("//AuthenticateResponse:AuthenticationRequirements/PostBack", nsmgr);
You should specify namespace for each element you are mentioning in xpath:
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "http://example.com/authentication/response/1");
var xpath = "/ns:AuthenticateResponse/ns:AuthenticationRequirements/ns:PostBack";
var node = doc.SelectSingleNode(xpath, nsmgr);
It should be:
var node = doc.SelectSingleNode("/AuthenticateResponse:AuthenticateResponse/AuthenticateResponse:AuthenticationRequirements/AuthenticateResponse:PostBack", nsmgr);
I have to analyze a XML doc with special namespace using C#, and I get some idea from this post. But my code fail to get the expected XML node, because the XML structure is very special...
There is a namespace in root node in XML
<MDOC xmlns="urn:schemas-microsoft-com/PSS/PSS_Survey01">
Here is my code to get this root node
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("urn", "schemas-microsoft-com/PSS/PSS_Survey01");
XmlNode root = doc.SelectSingleNode("MDOC", nsmgr);
Help me!
I am not sure what is special about your XML structure.
I would write the code little differently
string xmlNamespace = String.Empty;
XmlNamespaceManager nsmgr;
XmlNodeList nodeInfo = FABRequestXML.GetElementsByTagName("RootNodeName");
xmlNamespace = Convert.ToString(nodeInfo[0].Attributes["xmlns"].Value);
nsmgr = new XmlNamespaceManager(MyXml.NameTable);
nsmgr.AddNamespace("AB", xmlNamespace);
XmlNode myNode = MyXml.DocumentElement.SelectSingleNode("AB:NodeName", nsmgr);
Hope that helps
I have an XML file that looks like:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://www.someurl.com/somefile.xslt"?>
<AutoInsuranceClaim xmlns="http://www.someurl.com/schemas/AutoInsuranceClaim">
<Identification>
<BaseOwner>3</BaseOwner>
<BaseType>ABC123</BaseType>
<BaseTypeRef>471038341757</BaseTypeRef>
</Identification>
</AutoInsuranceClaim>
and I'm trying to read the Identification node. Here's my code:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(#"..\..\Data.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", "http://www.someurl.com/schemas/AutoInsuranceClaim");
XmlNodeList nodeList = xmlDoc.SelectNodes(#"/ns:AutoInsuranceClaim/Identification", nsmgr);
Console.WriteLine("There are {0} nodes...", nodeList.Count);
I know I should get a least 1 value. My understanding of .NET XML parsing is that if you have a default namespace with no prefix, you have to create your own namespace. But this should have returned 1.
If not, what am I missing?
I might be grasping at straws here, but shouldn't you be namespacing both entities in your xpath expression?
XmlNodeList nodeList = xmlDoc.SelectNodes(#"/ns:AutoInsuranceClaim/ns:Identification", nsmgr);
XElement root = XElement.Load("Data.xml");
var identifications = root.Descendants()
.Where(x => x.Name.LocalName == "Identification")
.ToList()
The problem is that you're trying to find an Identification node without a namespace, but it will have defaulted to the same namespace as the parent due to the xmlns=... part. Try this:
var nodeList = xmlDoc.SelectNodes("/ns:AutoInsuranceClaim/ns:Identification",
nsmgr);
Having tried it myself, it printed a count of 1.
Personally I'd use LINQ to XML instead though, which makes namespace easier handling:
XDocument doc = XDocument.Load(#"..\..\Data.xml");
XNamespace ns = "http://www.someurl.com/schemas/AutoInsuranceClaim";
var nodes = doc.Root.Elements(ns + "Identification");
i am trying to read the following and select a node in it
<ns1:OrderInfo xmlns:ns1="http://xxxxxx Some URL XXXX">
<pricing someAttrHere>
<childnodes>
</pricing>
</ns1:OrderInfo>
.
XmlDocument document = new XmlDocument();
document.Load(Server.MapPath("order.xml"));
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
manager.AddNamespace("ns1", "http://xxxxxx Some URL XXXX");
query = "/ns1:OrderInfo/pricing";
XmlNodeList nodeList = document.SelectNodes(query);
but it always give "Namespace Manager or XsltContext needed"
as you can see above i add namespace using XmlNamespaceManager and still give the error
please any help
You need to use your XmlNamespaceManager as well:
XmlNodeList nodeList = document.SelectNodes(query, manager);