I would like to know how to get the nodepath of an Xml file. I have pasted below. So I want to create a method which will take in a node path and return me the xml for the element the nodepath points to.
The problem is suppose I want xml for FormA1 which is under the element Member (which has Mouser0 under Member/MemberName/BusinessNameLine1Txt), how can i get it since the xml also has another Member element (although with a different 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>
So if I were to pass the method a path,
ReturnState/ReturnDataState/Form6/Body/Member/FormA1 and would like it to return
<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>
And not the other FormA1 under the different Member element. How would I do that ?
Thanks a lot in Advance !!
AJ.
You can use an XPath query like this:
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());
Because I used XPathSelectElement
instead of XPathSelectElements
, you'll only get the first matching element. If you want to choose your results based on other criteria, there are other tricks you can use with xpath, like the position()
selector.
.net has several ways to run XPath against XML document, one is mentioned in the other answer by StriplingWarrior, so I believe xpath is the way to go here. You can use XPath predicate ( [...]
) to return only elements that fulfill certain criteria. For example, you can use the following XPath expression to get only Member
elements having grand-child BusinessNameLine1Txt
with value equals "Mouser0"
:
Member[MemberName/BusinessNameLine1Txt='Mouser0']
Having said that, the complete XPath expression to get FormA1
elements of such Member
parent would be as follow :
//Member[MemberName/BusinessNameLine1Txt='Mouser0']/FormA1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.