解析文件最佳方法

时间:2014-06-20 08:18:55

标签: c# .net xml parsing xpath

我需要解析Service Manger中的以下文件,因为它看起来几乎像xml,除了它有两个xml不能的根元素。我正在询问有关如何最好地将此文件解析为keyvalue对的建议,谢谢。

一位朋友曾建议使用xpath解析它,如果有人可以提供一个例子,谢谢.net c#。

<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>

1 个答案:

答案 0 :(得分:1)

使用根节点包装格式错误的XML,然后将其视为有效的XML:

string originalXml;
......
......

string fixedXml = "<root>" + originalXml + "</root>";
XDocument doc = XDocument.Parse(fixedXml);