如何选择此XML节点并提取其属性

时间:2014-10-15 14:37:28

标签: c# xml regex wpf xml-parsing

我需要获得QueryPlanHash

它位于StmtSimple节点

如何使用C#.net 4.5 WPF应用程序执行此操作?

非常非常

因此,我希望获得0xB36E2AA500333529

我想也可以用正则表达式来完成

这里XML文件的某些部分非常大

<ShowPlanXML
    xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.2" Build="12.0.2254.0">
    <BatchSequence> 
        <Batch>
            <Statements>
                <StmtSimple StatementText="-- First query.&#xd;&#xa;select tblRoutes.routeId,tblUsersProfile.squareId,tblUsersProfile.PathFinding,routeName,shapeType,  maxColumn, maxRow, wildPokemonCatchRatio,ZoneNumber,ZoneName,RouteOrder from tblUsersProfile,tblRoutes,tblMapSquareFormat where  UserId=756537 AND tblRoutes.routeId=tblUsersProfile.routeId and tblMapSquareFormat.routeId=tblUsersProfile.routeId and tblMapSquareFormat.squareId=tblUsersProfile.squareId" StatementId="1" StatementCompId="1" StatementType="SELECT" RetrievedFromCache="false" StatementSubTreeCost="0.00985766" StatementEstRows="1" StatementOptmLevel="FULL" QueryHash="0xE96598585A24E1EE" QueryPlanHash="0xB36E2AA500333529" StatementOptmEarlyAbortReason="GoodEnoughPlanFound" CardinalityEstimationModelVersion="120">
                    <StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" NUMERIC_ROUNDABORT="false"/>
                    <QueryPlan CachedPlanSize="24" CompileTime="4" CompileCPU="4" CompileMemory="632">
                        <MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0"/>
                        <OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="418731" EstimatedPagesCached="209365" EstimatedAvailableDegreeOfParallelism="4"/>
                        <RelOp NodeId="0" PhysicalOp="Nested Loops" LogicalOp="Inner Join" EstimateRows="1" EstimateIO="0" EstimateCPU="4.18e-006" AvgRowSize="80" EstimatedTotalSubtreeCost="0.00985766" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
                            <OutputList>

4 个答案:

答案 0 :(得分:1)

以下是一种使用正则表达式的方法:

var match = Regex.Match(xmlContent, "QueryPlanHash=\"([^\"]+)\"", RegexOption.CultureInvariant); 

if (match.Success) 
{
    String queryPlanHashValue = match.Groups[1].Value;  // Contains "0xB36E2AA500333529" 
}

答案 1 :(得分:1)

一种非常简单的方法就是使用字符串拆分。

示例:

public string GetQueryPlanHash(string inputXML) {
   if(inputXML.Contains("QueryPlanHash=")) {
      var data1 = inputXML.Split(new[]{"QueryPlanHash=\""}, StringSplitOptions.None);
      return data1[1].Split('"')[0];
   }
   return null;
}

另一种方法是将XML作为XmlDocument或XDocument读取,并搜索属性并获取其值。使用XPath或递归搜索。 (无法展示一个很好的例子,因为我有一段时间没有这样做。)

答案 2 :(得分:1)

的LINQ / XML:

var doc = XElement.Parse(xml);
XNamespace ns = "http://schemas.microsoft.com/sqlserver/2004/07/showplan";

foreach (var stmnt in doc.Descendants(ns + "StmtSimple"))
{
    string value = (string)stmnt.Attribute("QueryPlanHash");
}

答案 3 :(得分:1)

(?<=QueryPlanHash=")[^"]*

试试这个。看看演示。

http://regex101.com/r/dZ1vT6/26