使用 XPath 读取 XML - 获取 XPathException
Reading XML with XPath - getting XPathException
我在使用 XPath 读取 xml 节点值时收到 XPathException?
代码片段:
XPathNavigator nav;
XPathDocument docNav;
string sampleXML = @"C:\sample.xml";
string ThumbnailXpath = @"//ern:NewReleaseMessage/media/asset/thumbnailURL/text()";
docNav = new XPathDocument(sampleXML);
nav = docNav.CreateNavigator();
thumbnailURL = nav.SelectSingleNode(ThumbnailXpath).Value;
XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ern:NewReleaseMessage xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xmlns:ern="http://ddex.net/xml/ern/37" ReleaseProfileVersionId="CommonReleaseTypes/12/AudioAlbum" LanguageAndScriptCode="en" xs:schemaLocation="http://ddex.net/xml/ern/37 http://ddex.net/xml/ern/37/release-notification.xsd" MessageSchemaVersionId="ern/37">
<IsBackfill>false</IsBackfill>
<media>
<asset id="050087105471">
<language>en_uk</language>
<grid/>
<cpCode/>
<firstReleaseDate/>
<territory/>
<thumbnailURL>placeholder</thumbnailURL>
<size/>
<description/>
</asset>
</media>
</ern:NewReleaseMessage>
异常
An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.
Additional information: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
正如错误所说,如果您的 XPath 使用名称空间,则需要使用名称空间管理器:
XPathNavigator nav;
XPathDocument docNav;
string sampleXML = @"C:\sample.xml";
string ThumbnailXpath = @"//ern:NewReleaseMessage/media/asset/thumbnailURL/text()";
XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
nsm.AddNamespace("ern", "http://ddex.net/xml/ern/37");
docNav = new XPathDocument(sampleXML);
nav = docNav.CreateNavigator();
thumbnailURL = nav.SelectSingleNode(ThumbnailXpath, nsm).Value;
我在使用 XPath 读取 xml 节点值时收到 XPathException?
代码片段:
XPathNavigator nav;
XPathDocument docNav;
string sampleXML = @"C:\sample.xml";
string ThumbnailXpath = @"//ern:NewReleaseMessage/media/asset/thumbnailURL/text()";
docNav = new XPathDocument(sampleXML);
nav = docNav.CreateNavigator();
thumbnailURL = nav.SelectSingleNode(ThumbnailXpath).Value;
XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ern:NewReleaseMessage xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xmlns:ern="http://ddex.net/xml/ern/37" ReleaseProfileVersionId="CommonReleaseTypes/12/AudioAlbum" LanguageAndScriptCode="en" xs:schemaLocation="http://ddex.net/xml/ern/37 http://ddex.net/xml/ern/37/release-notification.xsd" MessageSchemaVersionId="ern/37">
<IsBackfill>false</IsBackfill>
<media>
<asset id="050087105471">
<language>en_uk</language>
<grid/>
<cpCode/>
<firstReleaseDate/>
<territory/>
<thumbnailURL>placeholder</thumbnailURL>
<size/>
<description/>
</asset>
</media>
</ern:NewReleaseMessage>
异常
An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.
Additional information: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
正如错误所说,如果您的 XPath 使用名称空间,则需要使用名称空间管理器:
XPathNavigator nav;
XPathDocument docNav;
string sampleXML = @"C:\sample.xml";
string ThumbnailXpath = @"//ern:NewReleaseMessage/media/asset/thumbnailURL/text()";
XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
nsm.AddNamespace("ern", "http://ddex.net/xml/ern/37");
docNav = new XPathDocument(sampleXML);
nav = docNav.CreateNavigator();
thumbnailURL = nav.SelectSingleNode(ThumbnailXpath, nsm).Value;