Parsing XML
-
I am trying to drill down a xml file having schema reference in c# .NET and while processing have added namespace using XmlNamespaceManager but it still gives error "Namespace Manager or XsltContext needed. This query has a prefix, variable,or user-defined function." Following is the code snippet -----------------code snippet----------------------------------------- XmlDocument objDoc = new XmlDocument(); XmlNode objRoot; objDoc.Load("C:\\abc.xml"); XmlNamespaceManager objXmlNS = new XmlNamespaceManager(objDoc.NameTable); objXmlNS.AddNamespace("xmlns:OrigoNS","http://www.origoservices.com"); objRoot=objDoc.DocumentElement; string MsgID=objRoot.SelectSingleNode("//OrigoNS:message_id").InnerText ------------------- abc.xml ---------------------------------- 2005-07-25T09:55:30342C3E5F-xxx4-4073-Byyy-C543973AA2960abc Valuation Requestv1.0synchronousxyzabctest ----------------------------------------------------------------- Would appreciate your help if you have got any clue for this problem Thanks Share knowledge to enhance your learning
-
I am trying to drill down a xml file having schema reference in c# .NET and while processing have added namespace using XmlNamespaceManager but it still gives error "Namespace Manager or XsltContext needed. This query has a prefix, variable,or user-defined function." Following is the code snippet -----------------code snippet----------------------------------------- XmlDocument objDoc = new XmlDocument(); XmlNode objRoot; objDoc.Load("C:\\abc.xml"); XmlNamespaceManager objXmlNS = new XmlNamespaceManager(objDoc.NameTable); objXmlNS.AddNamespace("xmlns:OrigoNS","http://www.origoservices.com"); objRoot=objDoc.DocumentElement; string MsgID=objRoot.SelectSingleNode("//OrigoNS:message_id").InnerText ------------------- abc.xml ---------------------------------- 2005-07-25T09:55:30342C3E5F-xxx4-4073-Byyy-C543973AA2960abc Valuation Requestv1.0synchronousxyzabctest ----------------------------------------------------------------- Would appreciate your help if you have got any clue for this problem Thanks Share knowledge to enhance your learning
Hi Neel07, The Problem is the line:
objXmlNS.AddNamespace("xmlns:OrigoNS","http://www.origoservices.com");
You register the namespace uri http://www.origoservices.com with the prefix xmlns:OrigoNS. But you request uses the prefix OrigoNS only. Simple use following line:objXmlNS.AddNamespace("OrigoNS","www.origoservices.com");
Good Luck! Niedzi PS: If you use xml code in your message, please use the Symbols < and > of the formating line (above the smilies) for the angle brackets. Or write simple < and > for < and >. -
Hi Neel07, The Problem is the line:
objXmlNS.AddNamespace("xmlns:OrigoNS","http://www.origoservices.com");
You register the namespace uri http://www.origoservices.com with the prefix xmlns:OrigoNS. But you request uses the prefix OrigoNS only. Simple use following line:objXmlNS.AddNamespace("OrigoNS","www.origoservices.com");
Good Luck! Niedzi PS: If you use xml code in your message, please use the Symbols < and > of the formating line (above the smilies) for the angle brackets. Or write simple < and > for < and >.Thanks a lot I have found the solution and you are right we should mention only "OrigoNS" as namespace prefix. Also it should call the 1st overloaded method of SelectSingleNode passing the XmlNamespaceManager object in the 2nd parameter. Corrected code XmlDocument objXmlDoc = new XmlDocument(); objXmlDoc.Load("C:\\abc.xml"); objRoot=objXmlDoc.DocumentElement; XmlNamespaceManager objXmlNS = new XmlNamespaceManager(objXmlDoc.NameTable); objXmlNS.AddNamespace("OrigoNS","http://www.origoservices.com"); XmlNode objRoot; string MsgID=objRoot.SelectSingleNode("//OrigoNS:message_id",objXmlNS).InnerText; Share knowledge to enhance your learning