无法到达特定的 xml 节点
我想访问 x:Property 节点及其属性:
Xml:
<Activity mc:Ignorable="sap" x:Class="WebApplication3.work" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:s4="clr-namespace:System;assembly=System.ServiceModel" xmlns:sa="clr-namespace:System.Activities;assembly=System.Activities" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Text;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:Members>
<x:Property Name="number1" Type="InArgument(x:Int32)" />
<x:Property Name="number2" Type="InArgument(x:Int32)" />
<x:Property Name="total" Type="OutArgument(x:Int32)" />
</x:Members>
.......
</Activity>
C# 代码:
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
XmlNodeList elements = doc.SelectNodes("//Activity/x:Members/x:Property",manager);
不幸的是 elements 变量返回 0 节点。你能帮助我吗?
I want to reach x:Property nodes and their attributes:
Xml:
<Activity mc:Ignorable="sap" x:Class="WebApplication3.work" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:s4="clr-namespace:System;assembly=System.ServiceModel" xmlns:sa="clr-namespace:System.Activities;assembly=System.Activities" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Text;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:Members>
<x:Property Name="number1" Type="InArgument(x:Int32)" />
<x:Property Name="number2" Type="InArgument(x:Int32)" />
<x:Property Name="total" Type="OutArgument(x:Int32)" />
</x:Members>
.......
</Activity>
C# Code:
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
XmlNodeList elements = doc.SelectNodes("//Activity/x:Members/x:Property",manager);
Unfortunately elements variable returns with 0 node. Can you help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
Try this
问题在于该文档位于默认命名空间中。
在您的 XPath 表达式中:
名称
Activity
没有前缀,并且被 XPath 视为“无命名空间”。XPath 求值器尝试查找文档中“无命名空间”中的所有
Activity
元素,但失败 - 因此结果为 0。解决方案很简单:
只需将此行添加到您的代码中:
然后计算此 XPath 表达式:
The problem is that the document is in a default namespace.
In your XPath expression:
the name
Activity
is unprefixed and is considered by XPath to be in "no namespace".The XPath evaluator is trying to find all
Activity
elements in the document that are in "no namespace" and fails -- hence the 0 results.The solution is easy:
Just add this line to your code:
then evaluate this XPath expression: