我有一个xml,我正在删除列表中的节点。 xml的节点以单词'格式'开头。我想删除那些节点,但目前我的方法无法处理它。我目前正在使用XDocument和Linq删除节点。有人可以解释如何删除以“'格式”字样开头的节点。 输入xml是
<PolicyHdr>
<PolicyHdrData>
<PolicyHdrData>
<ChangeType>-1</ChangeType>
<CoreMessageObjectArray>
<HasMessage>False</HasMessage>
<Message />
<MessageCode />
<Severity>0</Severity>
</CoreMessageObjectArray>
<ID>c1be84b1-f081-4fa6-bb45-610adc176042</ID>
<PolicyPrefix />
<PolicyNumber>TSAZ00001019</PolicyNumber>
<DTCCDistTrxIDNum />
<Status id="00000000-0000-0000-0000-000000000004" code="22">Active</Status>
<IssueState id="00000000-0000-0000-0000-000000000006" code="OLI_USA_CA">California</IssueState>
<IssueCountry id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<AppReceivedDate>2016-09-12T08:55:28.0000000</AppReceivedDate>
<ApplicationDate>2016-09-12T00:00:00.0000000</ApplicationDate>
<Qualified id="0">False</Qualified>
<ProdModelID id="7a841184-484b-457d-8a24-8257984dc5f0" code="Paycheck_Term">Lifely Term</ProdModelID>
<LOB id="00000000-0000-0000-0000-000000000003" code="L">Traditional Life</LOB>
<CompanyID id="00000000-0000-0000-0000-000000000020" code="PLAZ">Pruco Life of AZ</CompanyID>
<ExchangeType id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<HasPendingTrx id="1">True</HasPendingTrx>
<PolicyEffDate>2016-09-12T00:00:00.0000000</PolicyEffDate>
<TamraTestInd id="0">False</TamraTestInd>
<IRC7702Test>CVAT</IRC7702Test>
<TSA id="0">False</TSA>
<CommOptionType id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<CommIntRateType id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<FixedIndexedMode id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<ModeRule>0</ModeRule>
<QualifiedCode id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<OriginalPolicyNumber />
<CertificateSuffix />
<ResidentState id="00000000-0000-0000-0000-000000000006" code="OLI_USA_CA">California</ResidentState>
<ExchangeFlag id="0">False</ExchangeFlag>
<BrokerageAccount />
<MarketSegment id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<SpecialMarket id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<ContributorType id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<PolicySwitch id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<PlanOption>CP15</PlanOption>
<PreventMEC id="0">False</PreventMEC>
<AppSignCity />
<AppSignState id="00000000-0000-0000-0000-000000000006" code="OLI_USA_CA">California</AppSignState>
<MembershipNum />
<AnnuityType />
<AgentMatching id="0">False</AgentMatching>
<SystematicWithdrawal id="0">False</SystematicWithdrawal>
<SpecialRequest />
<TIA90Days id="0">False</TIA90Days>
<TIA2Years id="0">False</TIA2Years>
<CashOnHand>0</CashOnHand>
<EstimatedTransferAmount>0</EstimatedTransferAmount>
<FreeLookPeriod>0</FreeLookPeriod>
<AppType id="00000000-0000-0000-0000-000000000004" code="EAPP">E-App</AppType>
<ReplacementPercent>1</ReplacementPercent>
<ModeOfSettlement id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<ExchangeBasis1035>0</ExchangeBasis1035>
<ExchangePrem1035Ext>0</ExchangePrem1035Ext>
<ExchangePrem1035Int>0</ExchangePrem1035Int>
<PriorMECStatus id="ffffffff-ffff-ffff-ffff-ffffffffffff" code="" />
<RateLockEffectiveDate>1900-01-01T00:00:00.0000000</RateLockEffectiveDate>
<ProductCUSIP />
<FreeLookEndDate>2016-10-12T00:00:00.0000000</FreeLookEndDate>
<FreeLookStartDate>2017-08-23T00:00:00.0000000</FreeLookStartDate>
<ReplacementFlag id="0">False</ReplacementFlag>
<EntityStatus id="0">False</EntityStatus>
<DisplayFieldName>TSAZ00001019,9/12/2016 12:00:00 AMActiveCalifornia</DisplayFieldName>
<UserID>cb177837-37c0-4033-88d9-a40853a2a342</UserID>
<TimeStamp>2017-08-23T16:28:12.2030000</TimeStamp>
<FormatCashOnHand>$0.00</FormatCashOnHand>
<FormatEstimatedTransferAmount>$0.00</FormatEstimatedTransferAmount>
</PolicyHdrData>
</PolicyHdrData>
</PolicyHdr>
这是我的代码
private static string RemoveUnnecessaryNodes(string inputXml)
{
// may move those nodes to a configuration file
string[] unnecessaryNodes = new string[] { "ChangeType", "UserID", "TimeStamp", "DisplayFieldName", "CoreMessageObjectArray" , "WriteOpt", "EntityStatus", "ID", "DisplayName", "Sequence"};
XDocument doc = XDocument.Parse(inputXml);
foreach (string unnecessaryNode in unnecessaryNodes)
{
doc.Descendants().Where(e => e.Name == unnecessaryNode).Remove();
/*string pattern = BuildRegexPattern(unnecessaryNode);
inputXml = RemoveUnnecessaryNode(inputXml, pattern);*/
}
return doc.ToString();
}
答案 0 :(得分:1)
如何删除以单词&#39;格式&#39;开头的节点?
XDocument doc = XDocument.Parse(inputXml);
doc.XPathSelectElements("//*[starts-with(name(), 'Format')]").Remove();