繁体   English   中英

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

[英]How to select this XML node and extract its attribute

我需要获取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>

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

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

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

一种超级简单的方法是只使用字符串拆分。

例:

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或递归搜索。 (由于我有一段时间没有这样做,所以无法显示一个很好的例子)。

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");
}
(?<=QueryPlanHash=")[^"]*

试试看。看演示。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM