XmlNamespaceManager provided, but still get "Namespace Manager or XsltContext needed" - c#

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

Related

How to get xml tag which is in DataSet1 tag(C#)? [duplicate]

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.

Using underscore and period with XPath

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']");

parse xml that contain colon

I have this XML text:
<test:solution xmlns:test="http://www.test.com/">
<script/>
<test:question>
<test:param name="name1">value 1</test:param>
<test:param name="name2"> value 2</test:param>
</test:question>
</test:solution>
when I run my application that contains those lines:
XmlDocument doc = new XmlDocument();
doc.Load(xmlUrl);
XmlNode testQuestions = doc.SelectSingleNode("/test:solution/test:question");
//XmlNodeList testParamNodeList = testQuestions.SelectNodes("test:param");
It give an error, I know that the error come from the colons, but I don't know how to resolve it.
Thanks for help
try this:
XmlDocument doc = new XmlDocument();
doc.Load(PATH);
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("test", "http://www.test.com/");
XmlNode node = doc.SelectSingleNode("//test:solution//test:question", ns);

Cannot select an XML node with XML namespace using SelectSingleNode method

I have the following XML
<?xml version="1.0" encoding="UTF-8"?>
<form:Documents xmlns:form="http://www.example.com/file.xsd" xmlns:addData="http://www.example.com/file2.xsd">
<_colored:_colored addData:DocumentState="Correct" xmlns:_colored="http://www.example.com/colored.xsd">
<_colored>
<_Field1>PB8996MT</_Field1>
</_colored>
</_colored:_colored>
</form:Documents>
I try to get the inner text of the _Field1 tag by writing the following C# code:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(filePath);
string fieldValue = xmlDocument.SelectSingleNode("/form:Documents/_colored:_colored/_colored/_Field1").InnerText;
And when I run the application I get the following exception:
Unhandled Exception: System.Xml.XPath.XPathException: Namespace Manager or XsltContext needed.
This query has a prefix, variable, or user-defined function.
You should declare the namespace prefix using an XmlNamespaceManager before you can use it in XPath expressions.
XmlDocument doc = new XmlDocument ();
doc.Load("/Users/buttercup/Projects/23564466/kram.xml");
XmlNamespaceManager nmspc = new XmlNamespaceManager(doc.NameTable);
nmspc.AddNamespace("form", "http://www.example.com/file.xsd");
nmspc.AddNamespace("addData", "http://www.example.com/file2.xsd");
nmspc.AddNamespace("_colored", "http://www.example.com/colored.xsd");
string fieldValue = doc.SelectSingleNode("/form:Documents/_colored:_colored/_colored/_Field1", nmspc).InnerText;
http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.aspx
LINQ to Xml can make things easier:
XDocument doc = XDocument.Load(filePath);
var value = doc.Descendants("_Field1").First().Value;

How to Select XML Nodes with XML Namespaces with C#

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

Categories