开发者

Can't reach to specific xml nodes

开发者 https://www.devze.com 2023-03-05 18:14 出处:网络
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

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.开发者_JS百科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?


var xdocument = XDocument.Load(filePath);
var xname = XName.Get("Property", "http://schemas.microsoft.com/winfx/2006/xaml");
var propertyNodes = xdocument.Descendants(xname).ToList();


Try this

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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号