简体   繁体   English

从Xml获取节点路径

[英]Getting the Node Path from Xml

I would like to know how to get the nodepath of an Xml file. 我想知道如何获取Xml文件的nodepath。 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. 因此,我想创建一个方法,该方法将采用节点路径,并向我返回节点路径指向的元素的xml。

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). 问题是假设我想要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>

So if I were to pass the method a path, 因此,如果我要将方法传递给路径,

ReturnState/ReturnDataState/Form6/Body/Member/FormA1 and would like it to return 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>

And not the other FormA1 under the different Member element. 而不是其他Member元素下的其他FormA1。 How would I do that ? 我该怎么做?

Thanks a lot in Advance !! 非常感谢预先!

AJ. AJ。

You can use an XPath query like this: 您可以使用如下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());

Because I used XPathSelectElement instead of XPathSelectElements , you'll only get the first matching element. 因为我使用XPathSelectElement而不是XPathSelectElements ,所以您只会得到第一个匹配的元素。 If you want to choose your results based on other criteria, there are other tricks you can use with xpath, like the position() selector. 如果要基于其他条件选择结果,则可以将其他技巧与xpath一起使用,例如position()选择器。

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. 有几种针对XML文档运行XPath的方法,StriplingWarrior 在另一个答案中提到了一种方法,因此我相信xpath是解决问题的方法。 You can use XPath predicate ( [...] ) to return only elements that fulfill certain criteria. 您可以使用XPath谓词( [...] )仅返回满足某些条件的元素。 For example, you can use the following XPath expression to get only Member elements having grand-child BusinessNameLine1Txt with value equals "Mouser0" : 例如,您可以使用以下XPath表达式仅获取具有"Mouser0" BusinessNameLine1Txt且其值等于"Mouser0" Member元素:

Member[MemberName/BusinessNameLine1Txt='Mouser0']

Having said that, the complete XPath expression to get FormA1 elements of such Member parent would be as follow : 话虽如此,获取此类Member父级的FormA1元素的完整XPath表达式如下:

//Member[MemberName/BusinessNameLine1Txt='Mouser0']/FormA1

xpathtester.com demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM