C# 如何选择此XML节点并提取其属性
我需要获取C# 如何选择此XML节点并提取其属性,c#,xml,regex,wpf,xml-parsing,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">
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.
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");
}
(?使用agilitypack或您想要的xml解析器,这将更容易(也更合适)。问题出在哪里?您可以通过键入代码来解决。。。
(?<=QueryPlanHash=")[^"]*