无法到达特定的 xml 节点

发布于 2024-11-06 22:01:51 字数 2118 浏览 3 评论 0原文

我想访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸 2024-11-13 22:01:51
var xdocument = XDocument.Load(filePath);
var xname = XName.Get("Property", "http://schemas.microsoft.com/winfx/2006/xaml");
var propertyNodes = xdocument.Descendants(xname).ToList();
var xdocument = XDocument.Load(filePath);
var xname = XName.Get("Property", "http://schemas.microsoft.com/winfx/2006/xaml");
var propertyNodes = xdocument.Descendants(xname).ToList();
剩一世无双 2024-11-13 22:01:51

试试这个

XmlNodeList elemList = doc.GetElementsByTagName("x:Property");

Try this

XmlNodeList elemList = doc.GetElementsByTagName("x:Property");
零崎曲识 2024-11-13 22:01:51

问题在于该文档位于默认命名空间中

在您的 XPath 表达式中:

//Activity/x:Members/x:Property

名称 Activity 没有前缀,并且被 XPath 视为“无命名空间”。

XPath 求值器尝试查找文档中“无命名空间”中的所有 Activity 元素,但失败 - 因此结果为 0。

解决方案很简单

只需将此行添加到您的代码中:

manager.AddNamespace("def", "http://schemas.microsoft.com/netfx/2009/xaml/activities");

然后计算此 XPath 表达式

 XmlNodeList elements = doc.SelectNodes("//def:Activity/x:Members/x:Property",manager);

The problem is that the document is in a default namespace.

In your XPath expression:

//Activity/x:Members/x:Property

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:

manager.AddNamespace("def", "http://schemas.microsoft.com/netfx/2009/xaml/activities");

then evaluate this XPath expression:

 XmlNodeList elements = doc.SelectNodes("//def:Activity/x:Members/x:Property",manager);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文