标签: selectsinglenode

为什么XmlNamespaceManager是必需的?

我想出有点干,以为什么 -至少在.Net框架-这是必要使用XmlNamespaceManager,以处理命名空间(或相当笨重和详细[local-name()=...的XPath谓词/功能/等等)时执行XPath查询.我确实理解为什么命名空间是必要的或至少是有益的,但为什么它如此复杂?

为了查询简单的XML文档(没有命名空间)...

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootNode>
   <nodeName>Some Text Here</nodeName>
</rootNode>
Run Code Online (Sandbox Code Playgroud)

...可以使用类似的东西doc.SelectSingleNode("//nodeName")(可以匹配<nodeName>Some Text Here</nodeName>)

神秘#1:我的第一个烦恼 - 如果我理解正确 - 只是添加一个命名空间引用到父/ root标记(无论是否用作子节点标记的一部分),如下所示:

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootNode xmlns="http://someplace.org">
   <nodeName>Some Text Here</nodeName>
</rootNode>
Run Code Online (Sandbox Code Playgroud)

...需要几行额外的代码来获得相同的结果:

Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ab", "http://s+omeplace.org")
Dim desiredNode As XmlNode = doc.SelectSingleNode("//ab:nodeName", nsmgr)
Run Code Online (Sandbox Code Playgroud)

...本质上是梦想一个不存在的前缀(" ab")来找到一个甚至不使用前缀的节点.这有什么意义?有什么问题(概念上)doc.SelectSingleNode("//nodeName")

神秘#2:所以,假设你有一个使用前缀的XML文档:

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootNode xmlns:cde="http://someplace.org" xmlns:feg="http://otherplace.net">
   <cde:nodeName>Some Text Here</cde:nodeName>
   <feg:nodeName>Some Other Value</feg:nodeName> …
Run Code Online (Sandbox Code Playgroud)

.net xpath xml-namespaces selectsinglenode

66
推荐指数
3
解决办法
3万
查看次数

XmlDocument.SelectSingleNode和xmlNamespace问题

我正在将字符串加载到包含以下结构的XML文档:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">                  
  <ItemGroup>
    <Compile Include="clsWorker.cs" />        
  </ItemGroup>      
</Project>
Run Code Online (Sandbox Code Playgroud)

然后我将所有加载到xmldocument:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(Xml);
Run Code Online (Sandbox Code Playgroud)

然后出现以下问题:

XmlNode Node = xmldoc.SelectSingleNode("//Compile"); // return null
Run Code Online (Sandbox Code Playgroud)

当我从根元素(Project)中删除xmlns属性时,它的工作正常,如何改进我的SelectSingleNode以返回相关元素?

c# xml xmldocument selectnodes selectsinglenode

44
推荐指数
3
解决办法
6万
查看次数

在SelectSingleNode中使用XPath:从XML中检索单个元素(如果存在)

我的XML看起来像:

<?xml version=\"1.0\"?>
<itemSet>
       <Item>one</Item>
       <Item>two</Item>
       <Item>three</Item>
       .....maybe more Items here.
</itemSet>
Run Code Online (Sandbox Code Playgroud)

某些个别物品可能存在也可能不存在.假设我想要检索元素<Item>2,</Item>如果它存在.我尝试了以下XPath(在C#中).

  • XMLNode node = myXMLdoc.SelectSingleNode("/itemSet[Item='two']")---如果存在第2项,那么它只返回第一个第一个元素.也许这个查询只指向itemSet中的第一个元素,如果它有一个值为2的Item作为子元素.这种解释是否正确?

所以我尝试过:

  • XMLNode node = myXMLdoc.SelectSingleNode("/itemSet[Item='two']/Item[1]")---我读了这个查询,返回<Item>itemSet中第一个有value ='two'的元素.我对么?

这仍然只返回的第一个元素一个.我究竟做错了什么?在这两种情况下,使用兄弟姐妹我可以遍历子节点并获得两个,但这不是我正在看的.如果两个不存在,则SelectSingleNode返回null.因此,我获得一个成功的返回节点的事实确实表明存在元素二,所以如果我想要一个布尔测试来存在两个,上面的任何XPath就足够了,但实际上我需要完整的元素<Item>two</Item>作为我的返回节点.

[我的第一个问题,也是我第一次使用网络编程,所以我刚刚从过去的SO问题中学到了上面的XPath和相关的xml内容.所以请保持温和,让我知道如果我是一个doofus或蔑视任何社区规则.谢谢.]

c# xpath selectsinglenode

13
推荐指数
1
解决办法
5万
查看次数

Html Agility Pack SelectSingleNode在迭代中总是给出相同的结果?

我想要集合中的节点,但迭代SelectSingleNode我继续获得相同的对象只是node.Id正在改变...我尝试的是读出给定网站的web响应并捕获一些信息,如值,链接.. in特殊定义的元素.

int offSet = 0;
string address = "http://www.testsite.de/ergebnisliste.html?offset=" + offSet;

HtmlWeb web = new HtmlWeb();
//web.OverrideEncoding = Encoding.UTF8;
HtmlDocument doc = web.Load(address);

HtmlNodeCollection collection = doc.DocumentNode.SelectNodes("//div[@itemtype='http://schema.org/Posting']");

foreach (HtmlNode node in collection) {
    string id = HttpUtility.HtmlDecode(node.Id);
    string cpname = HttpUtility.HtmlDecode(node.SelectSingleNode("//span[@itemprop='name']").InnerText);
    string cptitle = HttpUtility.HtmlDecode(node.SelectSingleNode("//span[@itemprop='title']").InnerText);
    string cpaddress = HttpUtility.HtmlDecode(node.SelectSingleNode("//span[@itemprop='addressLocality']").InnerText);
    string date = HttpUtility.HtmlDecode(node.SelectSingleNode("//div[@itemprop='datePosted']").InnerText);
    string link = "http://www.testsite.de" + HttpUtility.HtmlDecode(node.SelectSingleNode("//div[@class='h3 title']//a[@href]").GetAttributeValue("href", "default"));               
}
Run Code Online (Sandbox Code Playgroud)

例如,这是1次迭代:

<div id="66666" itemtype="http://schema.org/Posting">   
<div>
    <a>
        <img />
    </a>
</div>
<div>
    <div class="h3 title">
        <a href="/test.html"  title="Test"> …
Run Code Online (Sandbox Code Playgroud)

html c# selectsinglenode html-agility-pack

13
推荐指数
1
解决办法
9706
查看次数

SelectSingleNode总是返回null?

以这个简化的XML示例为例:

<?xml version="1.0"?>
<message xmlns="http://www.mydomain.com/MyDataFeed" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mydomain.com/MyDataFeed https://secure.mydomain/MyDataFeed/myDataFeed.xsd" requestId="13898" status="1">
<error>Invalid Login</error>
</message>
Run Code Online (Sandbox Code Playgroud)

我试图使用SelectSingleNode方法选择'错误'节点,但是使用以下代码它总是返回NULL?

XmlNode errorNode = oss.SelectSingleNode("/message/error");
if (errorNode != null)
     Console.Writeline("There is an error");
Run Code Online (Sandbox Code Playgroud)

从我做过的研究来看,这与命名空间有关,但我根本无法得到任何工作.有什么建议?

c# xml xpath namespaces selectsinglenode

12
推荐指数
1
解决办法
6862
查看次数

xpath查询,用于查找具有与属性和子节点值匹配的条件的元素

我有2个元素具有相同的属性但具有不同的子节点值.我可以查询以查找与该属性匹配的特定元素以及子节点值.具体来说,这是我用来查询的示例xml(原始xml中的每个元素都有超过10个childe节点).

 <Book size="2">
  <Title>abc</Title>
  <Price>10</Price>
 </Book>
 <Book size="2">
  <Title>xyz</Title>
  <Price>20</Price>
 </Book>
 <Book size="4">
  <Title>Harry</Title>
  <Price>10</Price>
 </Book>
Run Code Online (Sandbox Code Playgroud)

所以,现在我想找到具有@size = "2"和的Book元素Title = xyz.

这可能通过使用SelectSingleNode方法吗?如果不是如何查询这个?

谢谢

xml xpath selectsinglenode

7
推荐指数
1
解决办法
2万
查看次数

为什么SelectSingleNode返回null?

我正在使用包含类似于此的结构的XML文档:

<MT>
  <Events>
    <event id="1">
      <field name="blah" value="a_value" type="atype" />
      .
      .
      .
     </event>
   </Events>
 </MT>
Run Code Online (Sandbox Code Playgroud)

我目前正以这种方式将文件从文件加载到XML文档中:

XmlDocument xdoc = new XmlDocument();
xdoc.Load("somefile.xml");  //Successfully loads btw
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试运行下一行代码时,我遇到了一个问题,只有这个特定的文档:

xdoc.SelectSingleNode("//event[@id='1']"); //This returns a null 
Run Code Online (Sandbox Code Playgroud)

我是否在正确的轨道上猜测这是因为使用名为'id'的属性的问题或者我在代码中遗漏了某些内容而返回null?

c# xml xmldocument xml-attribute selectsinglenode

6
推荐指数
1
解决办法
1万
查看次数

解析XML:我的元素中的冒号导致XPath错过它

我有一个XML文档,我加载并尝试使用XPath进行搜索.此文件中的根节点<t:Transmission xmlns:t='urn:InboundShipment'>已正确关闭文件末尾</t:Transmission>.

我的问题是我不能在不使用后代轴的情况下走树.换句话说,我可以这样做:SelectSingleNode("//TransactionHeader[SHIPPERSTATE='CA']")并获得一个节点作为回报.但是我不能做相同的事情:SelectSingleNode("/Transmission/TransmissionBody/Transaction/TransactionHeader[SHIPPERSTATE='CA']")

如果我删除了t:我可以执行XPath搜索/Transmission并获取整个文件.随着t:在那里我只是得到空.或者,如果我尝试SelectSingleNode("t:Transmission")我的XPath语句出错.

我通常不需要查询根元素,所以我应该能够使用后代轴进行搜索.但XML看起来对我有用,所以我想知道如何解决这个问题.另外,我不想让客户删除"t:"只是因为我不知道如何处理它.

c# xml xpath selectsinglenode

6
推荐指数
1
解决办法
9478
查看次数

包含正斜杠(/)的属性的XPath查询

我试图做SelectSingleNode一个关于XMLDocument使用XPath表达式.但是,当我搜索的属性值包含多个正斜杠(/)时,它返回null.

我找不到任何在线资源来逃避正斜杠.有没有人知道这方面的方法?或者我的语法错误匹配属性值?

示例XML

<?xml version="1.0"?>
  <Root>
    <Page Path="/brand" />
    <Page Path="/brand/armada" />
  </Root>
Run Code Online (Sandbox Code Playgroud)

此XPath表达式返回正确的节点(例如:上面示例中的第一个节点)

XmlNode N = xmlDoc.SelectSingleNode("Root/Page[@Path='/brand']");
Run Code Online (Sandbox Code Playgroud)

此XPath表达式返回null

XmlNode N = xmlDoc.SelectSingleNode("Root/Page[@Path='/brand/armada']");
Run Code Online (Sandbox Code Playgroud)

我在C#,.net 3.5环境中.

编辑:感谢您的回复.我通过在select表达式中使用双正斜杠解决了这个问题.

XmlNode N = xmlDoc.SelectSingleNode("Root//Page[@Path='/brand/armada']");
Run Code Online (Sandbox Code Playgroud)

c# xml xpath selectsinglenode

6
推荐指数
1
解决办法
5069
查看次数

.在Powershell脚本中选择单个节点,使用xPath无法从web.config文件中提取值

好的,所以这是我的web.config文件的片段:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<location path="." inheritInChildApplications="false">
<connectionStrings>
...
</connectionStrings>
</location>
<location path="." inheritInChildApplications="false">
<appSettings>
<!--IT Ops-->
<add key="SomeOtherKey" value="SomeOtherValue" />
<add key="SiteDomain" value="somedomain.com" />
<add key="SomeOtherKey" value="SomeOtherValue" />
....
</appSettings>
</location>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我要做的是通过Powershell使用xPath找到节点.有关此XML文件的一些注意事项:

有多个:

<location path="." inheritInChildApplications="false"> 
Run Code Online (Sandbox Code Playgroud)

xml文件中的值.他们围绕其他节点等...

我可以使用此脚本成功找到并替换连接字符串值

$WebConfigFile = Join-Path $destination Web.config
[xml]$WebConfigXml = Get-Content ($WebConfigFile)
$WebConfigXml.configuration.location[2].connectionStrings.add | % { $_.connectionString = $_.connectionString -replace "some value", $sqlServerName }
Run Code Online (Sandbox Code Playgroud)

但是当我使用这个脚本替换add key ="SiteDomain"值时:

$node = $WebConfigXml.configuration.location[3].appSettings.SelectSingleNode("add[@key = 'SiteDomain']")
$node.value = "someValue"
$WebConfigXml.Save($WebConfigFile)
Run Code Online (Sandbox Code Playgroud)

这是行不通的.在这种情况下,$ node值包含一个空字符串.

我也试图像这样读取节点:

$appSettingsSection = …
Run Code Online (Sandbox Code Playgroud)

powershell xpath web-config selectsinglenode

6
推荐指数
1
解决办法
2万
查看次数