查询带有索引路径的XMl

本文关键字:XMl 路径 索引 查询 | 更新日期: 2023-09-27 18:17:38

我有一个xml文件,我想使用索引路径查询。我不确定这是否可能,但任何帮助都是非常感谢的!

所以我要找的是能够查询xml文件的路径类似于这样。

ReturnState [0] ' ReturnDataState [0] ' Form6身体[0][0]' '[0]成员' FormA1

应该给我第一个成员元素下的FormA1。使用这种方法有很多原因,在不讨论太多细节的情况下,我想知道使用xpath或任何其他方式是否可以查询这样的内容。

<ReturnState> 
  <ReturnDataState>
    <Form6>    
      <Body>     
        <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>
        <Member>
              <MemberName>
                <BusinessNameLine1Txt>Mouser2</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>
        <Member>
          <MemberName>
            <BusinessNameLine1Txt>Mouser3</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>

谢谢,AJ

查询带有索引路径的XMl

Xpath有自己需要遵循的规范,它与您目前使用的路径表达式没有太大区别。这里的区别在于,XPath索引从1开始,而不是从0开始;XPath中的路径分隔符从/开始,而不是从'开始。最好稍微调整路径表达式以符合XPath语法,除非您愿意实现自己的解析器:

var doc = XDocument.Load("path_to_your_file.xml");
var xpath = "ReturnState[1]/ReturnDataState[1]/Form6[1]/Body[1]/Member[1]/FormA1";
var result = doc.XPathSelectElement(xpath);
Console.WriteLine(result.ToString());

Dotnetfiddle Demo

输出:

<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>