有人可以解释一下,为什么这不起作用?
我正在执行
XmlNode xmlNode = xmlDocument.SelectSingleNode("//(artist|author)");
Run Code Online (Sandbox Code Playgroud)
我明白了
System.Xml.XPath.XPathException: Expression must evaluate to a node-set.
但这有效,即使有很多艺术家节点也不会引发异常
XmlNode xmlNode = xmlDocument.SelectSingleNode("//artist");
Run Code Online (Sandbox Code Playgroud) 有一个xml文件
<DataSource>
<localdata>
<add context="Localization">
<parameter name="timeout" type="int" defaultvalue="60"/>
<parameter name="address" type="string" defaultvalue="192.168.9.45" />
<parameter name="port" type="int" defaultvalue="6789"/>
</add>
<add context="General">
<parameter name="timeout" type="int" defaultvalue="60"/>
<parameter name="address" type="string" defaultvalue="192.168.9.478" />
<parameter name="port" type="int" defaultvalue="5674"/>
</add>
</localdata>
</DataSource>
Run Code Online (Sandbox Code Playgroud)
我需要获取其属性context="General"使用vbscript 的元素
我可以用这个语句获得顶级节点
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.load("DataConfiguration.xml")
Set queryNode = xmlDocument.selectSingleNode(".//localdata")
Run Code Online (Sandbox Code Playgroud)
但不知道如何扩展这一点.
任何帮助表示赞赏.
提前致谢.
我一直在用这段代码打几个小时....
结果的示例节点:
<div class="left vcard" sizcache="1" sizset="32">
<h2 class="clearfix fn org url" sizcache="1" sizset="32">
<a id="listItemTitle_11310540" href="/marcali/viragok-viragkuldes/11310540/ANDOK_MATYAS/hirdetes.mtt">ANDÓK MÁTYÁS</a> <a class="removeFromList" href="#">törlés</a>
</h2>
<p class="description">
2009 decemberében nyitottuk meg az Ezerszirom Virágbolt-ot Marcaliban a Pet?fi Sándor u.11-ben. Szeretettel várja a kedves vásárlókat Horváth Györgyike virágköt?, aki 15 éve kápráztatja el kreatíva...</p>
<ul class="profession" sizcache="1" sizset="34">
<li sizcache="1" sizset="34"><a href="/szakmak/viragok-viragkuldes/index.mtt">Virágok, virágküldés</a> </li>
</ul>
<div class="clearfix margined" sizcache="1" sizset="35">
<p class="address adr">
<span>Cím:</span> 8700 Marcali, Pet?fi S. utca 11 .</p>
<ul class="nav clearfix" …Run Code Online (Sandbox Code Playgroud) 当我尝试使用RemoveChild()删除我的一些子元素时.但抛出异常.我在下面附上了我的代码.
nodeName = doc.SelectSingleNode("//Equipment//DataCollections//EnabledIDs//MyID[@id='" + attrValue + "']");
// Found the nodeName successfully druing run time.
doc.DocumentElement.RemoveChild(nodeName);
// faild to Remove the node
Run Code Online (Sandbox Code Playgroud)
显示以下错误:
An unhandled exception of type 'System.ArgumentException' occurred in System.Xml.dll
Additional information: The node to be removed is not a child of this node.
Run Code Online (Sandbox Code Playgroud)
如何删除节点?
[更新]
使用VS2005和.NET 2.0.
使用Javascript:
var req=xmlDoc.responseXML.selectSingleNode("//title");
alert(req.text);
Run Code Online (Sandbox Code Playgroud)
按预期,返回第一个"标题"节点的文本.
但是这个
var req=xmlDoc.responseXML.selectNodes("//title");
alert(req.text);
Run Code Online (Sandbox Code Playgroud)
返回"未定义".下列:
var req=xmlDoc.responseXML.selectNodes("//title").length;
alert(req);
Run Code Online (Sandbox Code Playgroud)
返回"2" 我不明白.也许当我选择节点时,它没有获得标题内的文本节点.这是我现在的猜测......这是xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<decal>
<company>Victor</company>
<title>Wood Horn Blue Background</title>
<image>
<url>victor01.jpg</url>
<width>60</width>
<height>60</height>
<name>Wood Horn Blue Background</name>
<link></link>
</image>
<price>$15.00</price>
<instock>In Stock</instock>
<notes>no extra info</notes>
</decal>
<decal>
<company>Victor</company>
<title>Wood Horn without Black Ring</title>
<image>
<url>victor02.jpg</url>
<width>60</width>
<height>60</height>
<name>Wood Horn Without Black Ring</name>
<link></link>
</image>
<price>$15.00</price>
<instock>In Stock</instock>
<notes>no extra info</notes>
</decal>
</catalog>
Run Code Online (Sandbox Code Playgroud)
谢谢
javascript internet-explorer xmlhttprequest selectnodes selectsinglenode
这个问题是对已回答问题的跟进: XmlDocument.SelectSingleNode和前缀+ xmlNamespace问题
问题是,将来可能会在没有警告的情况下更改收到的xml的名称空间前缀,因此我们想知道是否有任何方法可以使用SelectSingleNode但省略元素的前缀.
(我们知道我们可以删除传入xml的所有前缀,但它需要更多步骤....虽然如果提供代码我们会认为它是一个有效的答案......)
我有一个类似问题,即XPath忽略大小写的SelectNodes问题,但在我的情况下,大写/小写问题是在名称为“ application”的节点中(有时是“ Application”,有时是“ application”)。
我将如何应用其他帖子的解决方案?还是在这种情况下适用另一种?
xml:
<?xml version="1.0" encoding="utf-16" ?>
<application>
<forms>
<action type="update">
<form uid="" >
</form>
</action>
</forms>
</application>
Run Code Online (Sandbox Code Playgroud)
在C#3.5中:
XmlNode nodex= oXMLDoc1.SelectSingleNode("Application/forms/action/form/@uid")
nodex.Value="UniqueIDx";//nodex is null :S
Run Code Online (Sandbox Code Playgroud) 我正在尝试从以下 XML 文档 ( https://gdata.youtube.com/feeds/api/videos?q=example )获取视频的视图,我能够获取链接和作者,因为标签中没有冒号。
我正在尝试获取 yt:statistics 但我不知道如何获取。
result = e.Result.Replace("xmlns='http://www.w3.org/2005/Atom' ", String.Empty);
XmlDocument doc = new XmlDocument();
doc.LoadXml(result);
XmlNodeList videos = doc.GetElementsByTagName("entry");
foreach (XmlNode video in videos)
{
XmlNode insideauthor = video.SelectSingleNode("author");
string videoId = video.SelectSingleNode("id").InnerText.Replace("http://gdata.youtube.com/feeds/api/videos/", String.Empty);
string author = insideauthor.SelectSingleNode("name").InnerText;
// Trying to get the views of a video of the search results
MessageBox.Show(video.SelectSingleNode("yt:statistics").Attributes["viewCount"].InnerText);
}
Run Code Online (Sandbox Code Playgroud) 如果我有一个XML文档
<root a="value">
<item name="first">
x
<foo name = "firstgrandchild">There is nothing here</foo>
y
<foo name = "secondgrandchild">There is something here</foo>
</item>
<item name="second">
xy
<foo/>
ab
</item>
</root>
Run Code Online (Sandbox Code Playgroud)
我想首先找到节点"item"的第一个出现,然后更新属性,然后我想更新第一次出现的节点"foo",然后更新属性等,
我的代码如下
myDoc.Load("Items2.xml");
myNode = myDoc.DocumentElement;
mySearchNode = myNode.SelectSingleNode("/root/item");
mySearchNode.Attributes["name"].Value = "Joel";
Console.WriteLine(mySearchNode.OuterXml);
mySearchChildNode = mySearchNode.SelectSingleNode("/item/foo");
Console.WriteLine(mySearchChildNode.OuterXml);
Run Code Online (Sandbox Code Playgroud)
虽然,第一次搜索和更新属性工作正常,但第二次失败,因为mySearchNode.SelectSingleNode返回null.
问题 - 此代码是否存在根本问题?为什么SelectSingleNode在第二个实例中没有按预期工作,就它而言,我在类型为Element的XmlNode上执行它.
请帮助.
非常感谢,
c# ×6
xml ×6
xpath ×3
selectnodes ×2
c#-2.0 ×1
javascript ×1
removechild ×1
vbscript ×1
xmldocument ×1
xmlnode ×1