[英]Getting the Node Path from Xml
我想知道如何獲取Xml文件的nodepath。 我已粘貼在下面。 因此,我想創建一個方法,該方法將采用節點路徑,並向我返回節點路徑指向的元素的xml。
問題是假設我想要FormA1的xml在元素Member下(在Member / MemberName / BusinessNameLine1Txt下有Mouser0),由於xml也有另一個Member元素(盡管具有不同的BusinessNameLine1Txt = Mouser1),我如何獲取它。
<ReturnState>
<ReturnDataState>
<Form6>
<Header>
<BusinessActivityCode>423690</BusinessActivityCode>
<StateOfIncorporation>
<State>DE</State>
<YearOfIncorporation>2006</YearOfIncorporation>
</StateOfIncorporation>
</Header>
<Body>
<EstTaxPmtLessRefund>492823</EstTaxPmtLessRefund>
<Overpayment>118450</Overpayment>
<OverpaymentCreditedNxtYr>118450</OverpaymentCreditedNxtYr>
<Reconciliation></Reconciliation>
<SignatureArea></SignatureArea>
<Member>
<MemberName>
<BusinessNameLine1Txt>Mouser0</BusinessNameLine1Txt>
</MemberName>
<FormA1>
<PartI-SalesFactor>
<SalesDelOrShippedOutState>31754631</SalesDelOrShippedOutState>
<TotalSales>
<Wisconsin>31754631</Wisconsin>
<TotalCompany>1965873635</TotalCompany>
</TotalSales>
<SalesFactorTotal>
<Wisconsin>31754631</Wisconsin>
<TotalCompany>1965873635</TotalCompany>
</SalesFactorTotal>
<ApportionmentPercentage>0.000000</ApportionmentPercentage>
</PartI-SalesFactor>
</FormA1>
</Member>
<Member>
<MemberName>
<BusinessNameLine1Txt>Mouser1</BusinessNameLine1Txt>
</MemberName>
<FormA1>
<PartI-SalesFactor>
<SalesDelOrShippedOutState>31754632</SalesDelOrShippedOutState>
<TotalSales>
<Wisconsin>31754632</Wisconsin>
<TotalCompany>1965873633</TotalCompany>
</TotalSales>
<SalesFactorTotal>
<Wisconsin>31754632</Wisconsin>
<TotalCompany>196587344</TotalCompany>
</SalesFactorTotal>
<ApportionmentPercentage>1.000000</ApportionmentPercentage>
</PartI-SalesFactor>
</FormA1>
</Member>
</Body>
</Form6>
</ReturnDataState>
</ReturnState>
因此,如果我要將方法傳遞給路徑,
ReturnState / ReturnDataState / Form6 / Body / Member / FormA1,並希望它返回
<FormA1>
<PartI-SalesFactor>
<SalesDelOrShippedOutState>31754631</SalesDelOrShippedOutState>
<TotalSales>
<Wisconsin>31754631</Wisconsin>
<TotalCompany>1965873635</TotalCompany>
</TotalSales>
<SalesFactorTotal>
<Wisconsin>31754631</Wisconsin>
<TotalCompany>1965873635</TotalCompany>
</SalesFactorTotal>
<ApportionmentPercentage>0.000000</ApportionmentPercentage>
</PartI-SalesFactor>
</FormA1>
而不是其他Member元素下的其他FormA1。 我該怎么做?
非常感謝預先!
AJ。
您可以使用如下XPath查詢:
var xElement = XElement.Parse(str); //or however you load your xml document
var formA1 = xElement.XPathSelectElement("ReturnDataState/Form6/Body/Member/FormA1");
Console.WriteLine(formA1.ToString());
因為我使用XPathSelectElement
而不是XPathSelectElements
,所以您只會得到第一個匹配的元素。 如果要基於其他條件選擇結果,則可以將其他技巧與xpath一起使用,例如position()
選擇器。
.net有幾種針對XML文檔運行XPath的方法,StriplingWarrior 在另一個答案中提到了一種方法,因此我相信xpath是解決問題的方法。 您可以使用XPath謂詞( [...]
)僅返回滿足某些條件的元素。 例如,您可以使用以下XPath表達式僅獲取具有"Mouser0"
BusinessNameLine1Txt
且其值等於"Mouser0"
Member
元素:
Member[MemberName/BusinessNameLine1Txt='Mouser0']
話雖如此,獲取此類Member
父級的FormA1
元素的完整XPath表達式如下:
//Member[MemberName/BusinessNameLine1Txt='Mouser0']/FormA1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.