1
我有一個XML,我正在刪除列表中存在的節點。 xml的節點以'Format'開頭,我想刪除這些節點,但目前我的方法無法處理它。我目前使用XDocument和Linq來刪除節點。可能有人解釋如何可以刪除其字「格式」開始的節點 輸入XML是如何從Xdocument中刪除節點而不指定節點的全名
<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();
}
你嘗試'doc.Descendants()。其中(E =>電子.Name.StartsWith(「Format」))。Remove();'? –
VisualStudio給了我一個錯誤,我不認爲StartsWith是linq – Hitzz91
中的函數或者,也可以將它們添加到不必要的節點列表中。 「格式」的沒有列出。 (並且「UserID」被列出兩次) – Kevin