Methods for using XPath with namespaces in XML under. Net

Source: Internet
Author: User
Tags net string xmlns xpath xslt
Xml

These two days are programmed for an RSS interface provided by another company, because they provide 1.0 version of RSS, where the XML part of the namespace, for this day and night for this, fell a lot of tumbling, Unicode coding problems, XPath problems, XSLT parsing problems. The following is a list of the solutions to these problems for the convenience of those who come later.

The problem with XPath: Because of the namespace, your XPath requires additional settings through the XmlNamespaceManager class. This method is I am in: http://www.error-bank.com/microsoft.public.dotnet.xml__35_r5x1v5$dha.2040@tk2msftngp12.phx.gbl_ Thread.aspx See, where the solution to this problem is:

Oleg Tkachenko [XML MVP, Xmlinsider]
Http://blog.tkachenko.com

Solved. Thank you very much for his answer there, so I found a solution. Here's one of my sample codes:

string ww = @ "<item rdf:about=" "http://ncsp.forum.nokia.com/download/?asset_id=10357"
xmlns:rdf= "" http://www.w3.org/1999/02/22-rdf-syntax-ns# ""
Xmlns:dc= "" HTTP://PURL.ORG/DC/ELEMENTS/1.1/""
xmlns:ncsp= "" http://ncsp.forum.nokia.com/"" >
<title>advanced Audio Support v1.0</title>
<link>http://ncsp.forum.nokia.com/download/?asset_id=10357</link>
<description>media Server Audio Streaming plug-in installation file for Nokia 9200 Series. Deprecated-use the official audio stream plug-in instead, available from SDK v1.2 and later.</description>
<dc:title xmlns:dc= "" HTTP://PURL.ORG/DC/ELEMENTS/1.1/"" >advanced Audio Support v1.0</dc:title>
<dc:identifier xmlns:dc= "HTTP://PURL.ORG/DC/ELEMENTS/1.1/" ">10357</dc:identifier>
<dc:language xmlns:dc= "HTTP://PURL.ORG/DC/ELEMENTS/1.1/" ">English</dc:language>
<ncsp:filesize xmlns:ncsp= "" http://ncsp.forum.nokia.com/"" >7 kb</ncsp:filesize>
<ncsp:class xmlns:ncsp= "http://ncsp.forum.nokia.com/" ">Toolkit</ncsp:class>
</item> ";
XmlReader Read = new XmlTextReader (new StringReader (WW));
XPathDocument doc = new XPathDocument (read);
XPathNavigator nav = doc. CreateNavigator ();
String strXPath = "/item/dc:identifier";
XPathExpression expr = Nav.compile (strXPath);
XmlNamespaceManager Mngr = new XmlNamespaceManager (nav. NameTable);
Mngr. AddNamespace ("DC", "HTTP://PURL.ORG/DC/ELEMENTS/1.1/");
Mngr. AddNamespace ("NCSP", "http://ncsp.forum.nokia.com/");
Expr. SetContext (MNGR);
XPathNodeIterator iter = nav. Select (expr);
string strval = null;
if (ITER). MoveNext ()) {
Strval = iter. Current.value;
}

XML+XSLT resolution of the problem, out of this problem, the last found is in the head of XML, more defined a namespace caused by the deletion of this namespace can be. Because of these RSS, is provided by other companies, the harm I find their own problems, looking for a long time.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.