解析xml文件不能选择节点
本文关键字:选择 节点 不能 文件 xml 解析 | 更新日期: 2023-09-27 18:09:34
我必须加载具有以下内容的xml文件,我添加了根节点视图,因为我没有xml文件不能有两个父母,但无论我使用以下方法做什么,我都不能选择路径来获得此函数的工作。这是使用c# .net 4
string text = System.IO.File.ReadAllText(@"XMLFile.XML");
XmlDocument currentDocument = new XmlDocument();
try
{
currentDocument.LoadXml(text);
}
catch (Exception ex)
{
throw ex;
}
string path = "View/Data";
XmlNodeList nodeList = currentDocument.SelectNodes(path);
IDictionary<string, string> keyValuePairList = new Dictionary<string, string>();
foreach (XmlNode node in nodeList)
{
foreach (XmlNode innerNode in node.ChildNodes)
{
if (innerNode.Attributes != null && innerNode.Attributes.Count == 2)
{
keyValuePairList.Add(new KeyValuePair<string, string>(innerNode.Attributes[0].Value, innerNode.Attributes[1].Value));
}
}
}
foreach (KeyValuePair<string, string> pair in keyValuePairList)
{
Console.WriteLine(string.Format("{0} : {1}", pair.Key, pair.Value));
}
Console.ReadLine();
Xml文件内容
<view>
<Data>
<Adapters>
<Adapter AdapterName="dataportal:EnterpriseManagementObjectProjectionAdapter">
<AdapterAssembly>Microsoft.EnterpriseManagement.UI.SdkDataAccess</AdapterAssembly>
<AdapterType>Microsoft.EnterpriseManagement.UI.SdkDataAccess.DataAdapters.EnterpriseManagementObjectProjectionAdapter</AdapterType>
</Adapter>
<Adapter AdapterName="viewframework://Adapters/AdvancedList">
<AdapterAssembly>Microsoft.EnterpriseManagement.UI.ViewFramework</AdapterAssembly>
<AdapterType>Microsoft.EnterpriseManagement.UI.ViewFramework.AdvancedListSupportAdapter</AdapterType>
</Adapter>
<Adapter AdapterName="omsdk://Adapters/Criteria">
<AdapterAssembly>Microsoft.EnterpriseManagement.UI.SdkDataAccess</AdapterAssembly>
<AdapterType>Microsoft.EnterpriseManagement.UI.SdkDataAccess.DataAdapters.SdkCriteriaAdapter</AdapterType>
</Adapter>
</Adapters>
<ItemsSource>
<AdvancedListSupportClass xmlns="clr-namespace:Microsoft.EnterpriseManagement.UI.ViewFramework;assembly=Microsoft.EnterpriseManagement.UI.ViewFramework" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" DataTypeName="" AdapterName="viewframework://Adapters/AdvancedList" FullUpdateAdapter="dataportal:EnterpriseManagementObjectProjectionAdapter" DataSource="mom:ManagementGroup" IsRecurring="True" RecurrenceFrequency="{x:Static s:Int32.MaxValue}" FullUpdateFrequency="1" Streaming="true">
<AdvancedListSupportClass.Parameters>
<QueryParameter Parameter="TypeProjectionId" Value="$MPElement[Name='System.WorkItem.Incident.View.ProjectionType']$"/>
</AdvancedListSupportClass.Parameters>
</AdvancedListSupportClass>
</ItemsSource>
<Criteria>
<QueryCriteria xmlns="http://tempuri.org/Criteria.xsd" Adapter="omsdk://Adapters/Criteria">
<Criteria>
<FreeformCriteria>
<Freeform>
<Criteria xmlns="http://Microsoft.EnterpriseManagement.Core.Criteria/">
<Expression>
<And>
<Expression>
<SimpleExpression>
<ValueExpressionLeft>
<Property>$Context/Property[Type='CoreIncident!System.WorkItem.Incident']/TierQueue$</Property>
</ValueExpressionLeft>
<Operator>Equal</Operator>
<ValueExpressionRight>
<Value>$MPElement[Name="IncidentTierQueuesEnum.Tier2"]$</Value>
</ValueExpressionRight>
</SimpleExpression>
</Expression>
<Expression>
<SimpleExpression>
<ValueExpressionLeft>
<Property>$Context/Property[Type='CoreIncident!System.WorkItem.Incident']/Status$</Property>
</ValueExpressionLeft>
<Operator>NotEqual</Operator>
<ValueExpressionRight>
<Value>$MPElement[Name="CoreIncident!IncidentStatusEnum.Resolved"]$</Value>
</ValueExpressionRight>
</SimpleExpression>
</Expression>
<Expression>
<SimpleExpression>
<ValueExpressionLeft>
<Property>$Context/Property[Type='CoreIncident!System.WorkItem.Incident']/Status$</Property>
</ValueExpressionLeft>
<Operator>NotEqual</Operator>
<ValueExpressionRight>
<Value>$MPElement[Name="CoreIncident!IncidentStatusEnum.Closed"]$</Value>
</ValueExpressionRight>
</SimpleExpression>
</Expression>
<Expression>
<UnaryExpression>
<ValueExpression>
<GenericProperty Path="$Context/Path[Relationship='WorkItem!System.WorkItemAssignedToUser' SeedRole='Source']$">Id</GenericProperty>
</ValueExpression>
<Operator>IsNull</Operator>
</UnaryExpression>
</Expression>
</And>
</Expression>
</Criteria>
</Freeform>
</FreeformCriteria>
</Criteria>
</QueryCriteria>
</Criteria>
</Data>
<Presentation>
<Columns>
<mux:ColumnCollection xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:mux="http://schemas.microsoft.com/SystemCenter/Common/UI/Views/GridView" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:datebinding="clr-namespace:Microsoft.EnterpriseManagement.UI.Extensions;assembly=Microsoft.EnterpriseManagement.UI.Extensions">
<mux:Column Name="id" DisplayMemberBinding="{Binding Path=Id, Mode=OneWay}" Width="150" DisplayName="Header_Id" Property="Id$ReturnValueAsBigInt$" DataType="s:String"/>
<mux:Column Name="title" DisplayMemberBinding="{Binding Path=Title, Mode=OneWay}" Width="200" DisplayName="Header_Title" Property="Title" DataType="s:String"/>
<mux:Column Name="sDisplayName" DisplayMemberBinding="{Binding Path=Status.DisplayName, Mode=OneWay}" Width="100" DisplayName="Header_Status" Property="Status.DisplayName" DataType="s:String"/>
<mux:Column Name="priority" DisplayMemberBinding="{Binding Path=Priority, Mode=OneWay}" Width="100" DisplayName="Header_Priority" Property="Priority" DataType="s:Int32"/>
<mux:Column Name="targetTime" DisplayMemberBinding="{datebinding:DateBinding Path=TargetResolutionTime, Mode=OneWay}" Width="100" DisplayName="Header_Target_Resolution_Time" Property="TargetResolutionTime" DataType="s:DateTime"/>
<mux:Column Name="aDisplayName" DisplayMemberBinding="{Binding Path=AffectedUser.DisplayName, Mode=OneWay}" Width="150" DisplayName="Header_Affected_User" Property="AffectedUser.DisplayName" DataType="s:String"/>
<mux:Column Name="lastModified" DisplayMemberBinding="{Binding Path=$LastModified$, Mode=OneWay}" Width="150" DisplayName="Header_Last_Modified" Property="$LastModified$" DataType="s:DateTime"/>
</mux:ColumnCollection>
</Columns>
<ViewStrings>
<ViewString ID="Header_Id">$MPElement[Name="System.WorkItem.Incident.Queue.Tier2.Unassigned.View.Header_Id"]$</ViewString>
<ViewString ID="Header_Title">$MPElement[Name="System.WorkItem.Incident.Queue.Tier2.Unassigned.View.Header_Title"]$</ViewString>
<ViewString ID="Header_Status">$MPElement[Name="System.WorkItem.Incident.Queue.Tier2.Unassigned.View.Header_Status"]$</ViewString>
<ViewString ID="Header_Priority">$MPElement[Name="System.WorkItem.Incident.Queue.Tier2.Unassigned.View.Header_Priority"]$</ViewString>
<ViewString ID="Header_Target_Resolution_Time">$MPElement[Name="System.WorkItem.Incident.Queue.Tier2.Unassigned.View.Header_Target_Resolution_Time"]$</ViewString>
<ViewString ID="Header_Affected_User">$MPElement[Name="System.WorkItem.Incident.Queue.Tier2.Unassigned.View.Header_Affected_User"]$</ViewString>
<ViewString ID="Header_Last_Modified">$MPElement[Name="System.WorkItem.Incident.Queue.Tier2.Unassigned.View.Header_Last_Modified"]$</ViewString>
</ViewStrings>
</Presentation>
</view>
只需在路径中添加/
,使其成为绝对的
string text = System.IO.File.ReadAllText(@"XMLFile.XML");
XmlDocument currentDocument = new XmlDocument();
try
{
currentDocument.LoadXml(text);
}
catch (Exception ex)
{
throw ex;
}
string path = "//*"; // retieve all data
XmlNodeList nodeList = currentDocument.SelectNodes(path);
IDictionary<string, string> keyValuePairList = new Dictionary<string, string>();
foreach (XmlNode node in nodeList)
{
foreach (XmlNode innerNode in node.ChildNodes)
{
if (innerNode.Attributes != null && innerNode.Attributes.Count == 2)
{
keyValuePairList.Add(new KeyValuePair<string, string>(innerNode.Attributes[0].Value, innerNode.Attributes[1].Value));
}
}
}
foreach (KeyValuePair<string, string> pair in keyValuePairList)
{
Console.WriteLine(string.Format("{0} : {1}", pair.Key, pair.Value));
}
Console.ReadLine();
你忘记了路径中的第一个斜杠:
string path = "/View/Data";
第一个斜杠表示绝对路径。如果省略它,则表示"根据当前匹配的节点选择View/Data
",这与./View/Data
相同。但由于此时没有选择节点,因此它不会返回任何内容。
要返回所有数据节点,您可以使用//
后代选择器:
"//Data"
要选择所有元素,除了根元素,使用*
和//
:
"//*"