Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


XPATH y Default Namespace

From: AlhambraEidosDevelopment@-----------.---------.---
To: 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>


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent