Gismo wrote:
Uzytkownik "Martin Honnen" <ma*******@yaho o.de> napisal w wiadomosci
news:Oq******** *****@TK2MSFTNG P09.phx.gbl...
Gismo wrote:
I have got file raport.rld which is an XML file generated by MS
Reporting
Services.
The problem is:
in this file are tags from two different namespaces
xmlns="http://schemas.microso ft.com/sqlserver/reporting/2003/10/reportdefini
tion"
xmlns:rd="http://schemas.microso ft.com/SQLServer/reporting/reportdesigner"
when I'm trying to use XPATH to get <teble> tag it doesn't work. (the
result is empty)
I've removed All tegs like
<rd:TypeName >System.Int32 </rd:TypeName>
Now all is working fine but what to do when I have tags from two
different
namespaces ?
You need to declare a prefix for every namespace and then use the prefix
in your XPath expression. How you do that depends on the environment, if
you use XPath inside of an XSLT stylesheet you can simply declare the
prefixes with
<xsl:styleshe et
xmlns:rd="xmlns :rd="http://schemas.microso ft.com/SQLServer/reporting/reportd
esigner"
and then use
rd:TypeName
in your XPath expression.
If you are using an API like selectNodes/selectSingleNod e then you need
to call
xmlDocument.set Property("Selec tionNamespaces" ,
"xmlns:rd='http ://schemas.microso ft.com/SQLServer/reporting/reportdesigner' "
)
There is no method like setProperty.
Sorry, I thought you might be using MSXML but you use .NET, there indeed
is no such method.
With .NET you need an XmlNamespaceMan ager and declare the prefixes for
your XPath expressions and then pass that manager to the SelectNodes method.
For instance with the example document being
<?xml version="1.0" encoding="UTF-8"?>
<gods xmlns="http://vatican.va/2004/gods">
<god name="Kibo" />
<god name="Jaffo" />
<dv:devil xmlns:dv="http://vatican.va/2004/devils" name="Xibo" />
</gods>
the following successfully queries the document using XPath
using System;
using System.Xml;
public class Test20040402 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(@"test2004040 2.xml");
XmlNamespaceMan ager namespaceManage r = new
XmlNamespaceMan ager(xmlDocumen t.NameTable);
namespaceManage r.AddNamespace( "gs", "http://vatican.va/2004/gods");
namespaceManage r.AddNamespace( "dv", "http://vatican.va/2004/devils");
XmlNodeList gods = xmlDocument.Sel ectNodes("/gs:gods/gs:god",
namespaceManage r);
Console.WriteLi ne("Found {0} elements.", gods.Count);
foreach (XmlNode node in gods) {
Console.WriteLi ne(node.Name);
}
gods = xmlDocument.Sel ectNodes("/gs:gods/dv:devil", namespaceManage r);
Console.WriteLi ne("Found {0} elements.", gods.Count);
foreach (XmlNode node in gods) {
Console.WriteLi ne(node.Name);
}
}
}
--
Martin Honnen
http://JavaScript.FAQTs.com/