Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries [Thread Prev] >Thread Next - Re: XPATH y Default Namespace XPATH y Default NamespaceTo: NULL Date: 5/27/2009 8:42:00 AM Hi all, I have a XML file (see below) and XPath expression: string xpath = @"POLIZAS/ID_MEDIADOR"; The xml has default namespace... xmlns="http://www.reale.es/monitorizacionb2b/ejemplo1" How can I select node using my XPath 1.0 if I have defaultnamespace ??????? <POLIZAS xsi:schemaLocation="http://www.reale.es/monitorizacionb2b/ejemplo1 Polizas.xsd" xmlns="http://www.reale.es/monitorizacionb2b/ejemplo1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ID_MEDIADOR>12</ID_MEDIADOR> <NOMBRE_MEDIADOR>MEDIADOR B</NOMBRE_MEDIADOR> - <POLIZA> I use code in C# for get value after use XPath Expression, but I don't get any value. Any help, please ?? Thanks in advanced, greetings, regards Code in C# public static string PruebasXPath(byte[] datosXML, string valorXPath) { return result; XML file string result = string.Empty; if (datosXML == null) throw new ArgumentNullException("Archivo", "El Archivo del formato es nulo"); try { MemoryStream ms = new MemoryStream(datosXML); XmlReader input = XmlReader.Create(ms); XPathDocument doc = new XPathDocument(input); XPathNavigator nav = doc.CreateNavigator(); // Compile a standard XPath expression XPathExpression expr; expr = nav.Compile(valorXPath); XPathNodeIterator iterator = nav.Select(expr); while (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current.Clone(); result += "valor: " + nav2.Value; result += "InnerXml: " + nav2.InnerXml;} } return result; More test ... MemoryStream ms = new MemoryStream(datosXML); XmlReader input = XmlReader.Create(ms); XmlDocument docxml = new XmlDocument(); docxml.Load(input); //docxml.DocumentElement.LocalName = "POLIZA"; string valorXPath = ByteArrayToStr(datosXPath); //create prefix<->namespace mappings (if any) XmlNamespaceManager nsMgr = new XmlNamespaceManager(input.NameTable); string defaultns = nsMgr.DefaultNamespace; //nsMgr.AddNamespace("mb2b", "http://www.reale.es/monitorizacionb2b/ejemplo1"); //nsMgr.AddNamespace("mb2b", docxml.DocumentElement.NamespaceURI); nsMgr.AddNamespace(docxml.DocumentElement.Prefix, docxml.DocumentElement.NamespaceURI); XmlNode dateNode = docxml.SelectSingleNode(valorXPath, nsMgr); XmlNodeList nodes = docxml.SelectNodes(valorXPath, nsMgr); foreach (XmlNode node in nodes) { Console.WriteLine(node.OuterXml + "\n\n"); result += "" + node.InnerXml; //nav2.Value } XML file <?xml version="1.0" encoding="UTF-8" ?> - <!-- Sample XML file generated by XMLSpy v2007 (http://www.altova.com) --> - <POLIZAS xsi:schemaLocation="http://www.reale.es/monitorizacionb2b/ejemplo1 Polizas.xsd" xmlns="http://www.reale.es/monitorizacionb2b/ejemplo1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ID_MEDIADOR>12</ID_MEDIADOR> <NOMBRE_MEDIADOR>MEDIADOR B</NOMBRE_MEDIADOR> - <POLIZA> <ID_POLIZA>104</ID_POLIZA> <ID_TOMADOR>1001</ID_TOMADOR> <NOMBRE_TOMADOR>TOMADOR A</NOMBRE_TOMADOR> - <RIESGO_AUTO> <MARCA>FIAT</MARCA> <PATENTE>12345</PATENTE> </RIESGO_AUTO> </POLIZA> - <POLIZA> <ID_POLIZA>105</ID_POLIZA> <ID_TOMADOR>1004</ID_TOMADOR> <NOMBRE_TOMADOR>TOMADOR B</NOMBRE_TOMADOR> - <RIESGO_AUTO> <MARCA>BMW</MARCA> <PATENTE>123456</PATENTE> </RIESGO_AUTO> </POLIZA> </POLIZAS> | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
